码迷,mamicode.com
首页 > 移动开发 > 详细

Android将图片保存到相册并及时看到

时间:2015-03-05 14:32:21      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

  Android中将图片保存到SD卡中,相册里不会及时出现这张图片,因为没有及时更新其索引,一般需要开机几次。当然我们可以手动更新其索引。

      1,首先将文件保存到SD卡中。

     String filePath = "xxx"; //全路径

              saveImgToSDcard(filePath);

      2,增加Android 内部媒体索引。

    public boolean saveImgToGallery(String filePath) {

    boolean sdCardExist = Environment.getExternalStorageState().equals(

    android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在

    if (!sdCardExist)

      return false; 

      try {

        ContentValues values = new ContentValues();

        values.put("datetaken", new Date().toString());

        values.put("mime_type", "image/jpg");

        values.put("_data", filePath);

        Application app = DoctorApplication.getInstance();

        ContentResolver cr = app.getContentResolver();

        cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

      } catch (Exception e) {

        e.printStackTrace();

      }

      return true;

    }

    3,刷新filePath的上一级目录

    MediaScannerConnection.scanFile(MyLanJingCode.this,new String[] { Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()+ "/"+ filePath.getParentFile().getAbsolutePath() }, null,null);

 

    这样就能及时在相册中看到增加的图片了。

Android将图片保存到相册并及时看到

标签:

原文地址:http://www.cnblogs.com/lsc183/p/4315479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!