关于安卓高版本图片保存

内容如题,由于安卓10开始,官方开始对隐私相关内容进行修改,变得更加严格。
所以,对于隐私相关的内容,如文件保存等,就需要进行适配。
下面开始介绍如何进行图片在安卓高版本中适配。

开发环境:
win10+androidstudio4.4+jdk1.8
targetVersion 30

文末附相关代码链接

思路

(一)如何在保存图片,保存在哪个目录
(二)如何删除图片

实现

(一)保存图片:
如果是用于应用之间,或者图片是用于跨应用操作,如图片分享等等。建议保存在系统开发的目录,如系统的Picture目录。如果图片只用于应用内部,建议保存在应用内部的目录。
核心代码如下:
(1)应用保存在应用内目录核心代码:

 String rootPath = MediaPathManager.getInstance().getAppInnerRootPath(context);
        String savePath = rootPath + File.separator + relPath + File.separator;
        File fileDir = new File(savePath);
        if (!fileDir.exists()) {
            createFile(fileDir, false);
        }
        File f = new File(savePath + fileName);
        if (!fileDir.exists()) {
            createFile(f, true);
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            source.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            LogUtil.d("save app dir: " + (savePath + fileName));
            return savePath + fileName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";

注意,如果保存在应用内部目录,是不需要权限的,只需要创建好相关目录和文件即可。

(2)保存在系统相册目录相关代码:

 private static String saveMediaToSys(Context context, Bitmap bitmap, String dirType, String relativeDir,
                                         String filename, String mimeType, String description) throws IOException {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            //首先保存
            File saveDir = Environment.getExternalStoragePublicDirectory(dirType);
            saveDir = new File(saveDir, relativeDir);
            if (!saveDir.exists() && !saveDir.mkdirs()) {
                try {
                    throw new Exception("create directory fail!");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            File outputFile = new File(saveDir, filename);
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(outputFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            bos.close();
            //最后通知图库更新
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(outputFile)));
            return outputFile.getPath();
        } else {
            String path = (!TextUtils.isEmpty(relativeDir)) ?
                    (Environment.DIRECTORY_PICTURES + File.separator + relativeDir) :
                    Environment.DIRECTORY_PICTURES;
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
            contentValues.put(MediaStore.Images.Media.DESCRIPTION, description);
            contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, path);
            contentValues.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
            //contentValues.put(MediaStore.Images.Media.IS_PENDING,1)
            Uri external = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            Uri insertUri = context.getContentResolver().insert(external, contentValues);
            OutputStream fos = (OutputStream) null;
            if (insertUri != null) {
                try {
                    fos = context.getContentResolver().openOutputStream(insertUri);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
                fos.flush();
                fos.close();
            }
            return MediaUriUtils.getFilePathFromContentUri(context, insertUri);
        }
    }

从上述代码中可以看出,如果是低于安卓10,是直接通过file文件进行创建,并且通过广播通知系统相册进行刷新的。而在安卓10以上(包含)则是通过MediaStore和contentResolver方法,进行插入到系统,原理其实是创建一个uri后写入文件,不过这个过程系统已经帮我们处理好,开发者只需要调用相关api即可。

(二)删除图片
原理上,其实可以参考插入图片的逻辑,因为换汤不换药的。核心代码如下:

public static void deletePicWithUri(Activity activity, String path) {
        try {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                new File(path).delete();
            } else {
                try {
                    Uri imageUri = MediaUriUtils.getImageContentUri(activity, new File(path));
                    activity.getContentResolver().delete(imageUri, null, null);
                } catch (RecoverableSecurityException e1) {
                    //捕获 RecoverableSecurityException异常,发起请求
                    try {
                        ActivityCompat.startIntentSenderForResult(activity,
                                e1.getUserAction().getActionIntent().getIntentSender(),
                                CODE_REQ, null, 0, 0, 0, null);
                    } catch (IntentSender.SendIntentException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {

        }
    }

上述代码思路就是:如果低于安卓10版本,则进行file的操作,高于则进行uri的操作。

写在最后

(1)相关操作时,注意权限申请
(2)文件操作时,注意目录的创建,对象的释放
(3)耗时操作,注意放在子线程

代码连接

that’s all--------------------------------------------------------------------

20230303更新
上述代码中,实测还是会发现存在问题的。所以经过总结,测试。有了如下的更新:

核心代码如下:
安卓10以上:

 /**
     * 保存到系统--Q以后
     *
     * @param externalUrl MediaStore.Images.Media.EXTERNAL_CONTENT_URI
     *                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI
     */
    private static String saveToSystemAfterQ(Context context, ContentValues contentValues,
                                             String fileFullPath, Uri externalUrl) throws Exception {
        ContentResolver contentResolver = context.getContentResolver();
        File tempFile = new File(fileFullPath);
        Uri external = externalUrl;
        Uri uri = contentResolver.insert(external, contentValues);
        copyFileAfterQ(context, contentResolver, tempFile, uri);
        contentValues.clear();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            contentValues.put(MediaStore.MediaColumns.IS_PENDING, 0);
        }
        context.getContentResolver().update(uri, contentValues, null, null);
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
        return MediaUriUtils.getFilePathFromContentUri(context, uri);
    }
  /**
     * 获取图片的ContentValue--Q以后
     */
    public static ContentValues getContentValuesAfterQ(String rootPath, File paramFile, String mimeType,
                                                       String desc) {
        long timestamp = System.currentTimeMillis();
        ContentValues localContentValues = new ContentValues();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            localContentValues.put(MediaStore.Images.Media.RELATIVE_PATH, rootPath);
        }
        localContentValues.put(MediaStore.Images.Media.TITLE, paramFile.getName());
        localContentValues.put(MediaStore.Images.Media.DISPLAY_NAME, paramFile.getName());
        localContentValues.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            localContentValues.put(MediaStore.Images.Media.DATE_TAKEN, timestamp);
            localContentValues.put(MediaStore.Images.Media.ORIENTATION, 0);
        }
        localContentValues.put(MediaStore.Images.Media.DATE_MODIFIED, timestamp);
        if (!TextUtils.isEmpty(desc)) {
            localContentValues.put(MediaStore.Images.Media.DESCRIPTION, desc);
        }
        localContentValues.put(MediaStore.Images.Media.DATE_ADDED, timestamp);
        localContentValues.put(MediaStore.Images.Media.DATA, paramFile.getAbsolutePath());
        localContentValues.put(MediaStore.Images.Media.SIZE, paramFile.length());
        return localContentValues;
    }
  /**
     * 安卓Q以后文件复制
     */
    private static void copyFileAfterQ(Context context, ContentResolver localContentResolver,
                                       File tempFile, Uri localUri) throws IOException {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
                context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.Q) {
            //拷贝文件到相册的uri,android10及以上得这么干,否则不会显示。可以参考ScreenMediaRecorder的save方法
            OutputStream os = localContentResolver.openOutputStream(localUri);
            Files.copy(tempFile.toPath(), os);
            os.close();
            tempFile.delete();
        }
    }

对于安卓10之前的,就直接复制就好了,这里就不一一细说。
源码文件可关注公众号后,回复–“安卓图片保存”

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值