Android保存图片到系统相册【刷新媒体库,更新相册】

 

Adnroid中保存图片的方法可能有如下两种:

  • 第一种是调用系统提供的插入图库的方法:
    MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", "description");
    调用以上系统自带的方法会把bitmap对象保存到系统图库中,但是这种方法无法指定保存的路径和名称,上述方法的title、description参数只是插入数据库中的字段,真实的图片名称系统会自动分配。

或者

MediaStore.Images.Media.insertImage(getContentResolver(), "image path", "title", "description");

但是发现,还是不能在相册中查看已经保存到的图片,结果发现需要刷新系统图库

  • 更新系统图库的方法
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
    上面那条广播是扫描整个sd卡的广播,如果你sd卡里面东西很多会扫描很久,在扫描当中我们是不能访问sd卡,所以这样子用户体现很不好,所以下面我们还有如下的方法:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your path"))););

完整代码

  • 1、保存Bitmap到本地指定路径下
  • 2、通过广播,通知系统相册图库刷新数据
public class ImgUtils {
    //保存文件到指定路径
    public static boolean saveImageToGallery(Context context, Bitmap bmp) {
        // 首先保存图片
        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dearxy";
        File appDir = new File(storePath);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            //通过io流的方式来压缩保存图片
            boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
            fos.flush();
            fos.close();

            //把文件插入到系统图库
            //MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);

            //保存图片后发送广播通知更新数据库
            Uri uri = Uri.fromFile(file);
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            if (isSuccess) {
                return true;
            } else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

注意:
1、别忘了权限

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2、如果是6.0手机,记得先申请权限,拿到权限后,再保存,不然会失败,我就是在这里被坑了

注意! 

原有的更新图库的方法已经失效,应该采用最新的:

/**
  * 通知android媒体库更新文件夹
  *
  * @param filePath ilePath 文件绝对路径,、/sda/aaa/jjj.jpg
  */
 public void scanFile(Context context, String filePath) {
     try {
         MediaScannerConnection.scanFile(context, new String[]{filePath}, null,
                 new MediaScannerConnection.OnScanCompletedListener() {
                     public void onScanCompleted(String path, Uri uri) {
                         Log.i("*******", "Scanned " + path + ":");
                         Log.i("*******", "-> uri=" + uri);
                     }
                 });
     } catch (Exception e) {
         e.printStackTrace();
     }
 }

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值