设为首页 加入收藏

TOP

Android将图片保存到相册并及时看到
2015-07-16 12:57:04 来源: 作者: 【 】 浏览:10
Tags:Android 图片 保存 相册 及时 看到

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);


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


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Ant 命令行编译Android项目 下一篇二叉查找树转换成排序的双向链表

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: