【Android】版本适配-关于requestLegacyExternalStorage优化记录

任务需求

详见前文
版本的更替导致这个并不是被大佬们推荐使用;为了后期的适配,需要对此进行优化。
在这里插入图片描述

 <application android:requestLegacyExternalStorage="true">

官网介绍在这里插入图片描述

优化记录

  1. Android10 android:requestLegacyExternalStorage=“true“ 无效,仍然报没有权限

修改至如下存储路径:

File testfile = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsoluteFile();

参考资料-android将应用中图片保存到系统相册并显示
实际得到效果通过TM文件管理查看已经缓存到data文件中;只是系统相册没有读取到?!
在这里插入图片描述

图片依旧缓存到本地的data文件中。调整多次,现问题的核心是系统相册要正确读到这个文件并展示出来!
目前的相册缓存显示代码如下:

// 最后通知图库更新
      context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
              Uri.fromFile(new File(currentFile.getPath()))));

文件路径:
在这里插入图片描述

//加载
MediaStore.Images.Media.insertImage(context.getContentResolver(),
                        currentFile.getAbsolutePath(), currentFile.toString(), null);
//                最后通知图库更新
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                        Uri.fromFile(new File(currentFile.getPath()))));
                callBack.onDownLoadSuccess(bitmap);

完整实现代码:

    public void saveImageToGallery(Context context,ImageDownLoadCallBack imageDownLoadCallBack) {
        callBack = imageDownLoadCallBack;
        //获取二维码图片资源
        Bitmap bitmap = BitmapFactory.decodeStream(getClass().getResourceAsStream("/res/drawable/wechat_qr_consult.jpg"));
        // 文件路径资源加载
        File file = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
        String fileName = "Zego";//图片存储的文件夹名字
        File appDir = new File(file ,fileName);
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        fileName = System.currentTimeMillis() + ".jpg";
        currentFile = new File(appDir, fileName);
        FileOutputStream fos = null;

        //判断是否已经下载到相册中,如果存在直接回调存在即可,反之就进行存储在相册操作
        //进行存储操作
        try {
            fos = new FileOutputStream(currentFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
        }catch (FileNotFoundException e) {
            ToastUtil.showToast(context,"FileNotFoundException");
            e.printStackTrace();
        } catch (IOException e) {
            ToastUtil.showToast(context,"IOException");
            e.printStackTrace();
        } finally {
            //关闭文件流
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                ToastUtil.showToast(context,"关闭流失败");
                e.printStackTrace();
            }
        }
        //加载回调
        if (bitmap != null && currentFile.exists()) {
            //通知相册更新
            try {
                MediaStore.Images.Media.insertImage(context.getContentResolver(),
                        currentFile.getAbsolutePath(), currentFile.toString(), null);
            //最后广播图库更新
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                        Uri.fromFile(new File(currentFile.getPath()))));
                callBack.onDownLoadSuccess(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        } else {
            callBack.onDownLoadFailed();
        }
    }
**    public void saveImageToGallery(Context context,ImageDownLoadCallBack imageDownLoadCallBack) {
        callBack = imageDownLoadCallBack;
        //获取二维码图片资源
        Bitmap bitmap = BitmapFactory.decodeStream(getClass().getResourceAsStream("/res/drawable/wechat_qr_consult.jpg"));
        // 文件路径资源加载
        File file = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
        String fileName = "Zego";//图片存储的文件夹名字
        File appDir = new File(file ,fileName);
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        fileName = System.currentTimeMillis() + ".jpg";
        currentFile = new File(appDir, fileName);
        FileOutputStream fos = null;

        //判断是否已经下载到相册中,如果存在直接回调存在即可,反之就进行存储在相册操作
        //进行存储操作
        try {
            fos = new FileOutputStream(currentFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
        }catch (FileNotFoundException e) {
            ToastUtil.showToast(context,"FileNotFoundException");
            e.printStackTrace();
        } catch (IOException e) {
            ToastUtil.showToast(context,"IOException");
            e.printStackTrace();
        } finally {
            //关闭文件流
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                ToastUtil.showToast(context,"关闭流失败");
                e.printStackTrace();
            }
        }
        //加载回调
        if (bitmap != null && currentFile.exists()) {
            //通知相册更新
            try {
                MediaStore.Images.Media.insertImage(context.getContentResolver(),
                        currentFile.getAbsolutePath(), currentFile.toString(), null);
            //最后广播图库更新
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                        Uri.fromFile(new File(currentFile.getPath()))));
                callBack.onDownLoadSuccess(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        } else {
            callBack.onDownLoadFailed();
        }
    }

现改变存储位置,不走之前的方式

//        File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();//注意小米手机必须这样获得public绝对路径

try-with-resources

数据流读取的时候最好要采用如下的控制,这个也是java7的特性。文件读取的时候要习惯使用这种写法,交由系统进行close。

 //try-with-resources控制资源提高安全意识
        try(FileOutputStream fos = new FileOutputStream(currentFile)){
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值