你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2015-04-27] 来源:Linux社区  作者:lsc183 [字体:大 中 小]

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相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-04/116748.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款