如何解决:所有文件访问权限策略:不需要访问设备存储错误。

huangapple go评论138阅读模式
英文:

How to solve : All Files Access Permission policy : Access to device storage not required error

问题

我的Android应用最近因为这个错误被拒绝了,此构建中的更改是我使用了CameraX并创建了自定义相机,因此在拍照/录像后,我想将其写入文件并保存'URI',以便我可以在后台上传。是否有更安全/更好的方法来将我的图像保存到由Google推广的用户文件目录?

英文:

My android application recently got rejected with this error, The change in this build is that I have used CameraX and created a custom camera , so post taking a picture/video, I want to write it a file and save the 'URI' so that I can upload this in background. Is there are safer/better way to save my image to user's file directory that is promoted by Google

答案1

得分: 1

检查您的应用是否可以在不需要所有文件访问权限的情况下运行。因为如果您仅在操作媒体文件时使用该权限,那么您可能不需要它。

尝试将其替换为 SAF(Storage Access Framework)或 Mediastore API。这样,您就完全不需要“所有文件访问权限”。

根据您的用例,如果要将媒体文件保存到外部存储器中,您可以使用 Mediastore 轻松实现。

您可以将位图保存到存储器并像这样更新 MediaStore,

public static Uri saveBitmap(Context context,
                             Bitmap bitmap,
                             Bitmap.CompressFormat compressFormat,
                             String mime_type,
                             String display_name,
                             String path) {
    OutputStream openOutputStream;
    ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, display_name);
    contentValues.put(MediaStore.Images.ImageColumns.MIME_TYPE, mime_type);
    contentValues.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, path);
    ContentResolver contentResolver = context.getContentResolver();
    Uri insert = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
    if (insert != null) {
        try {
            openOutputStream = contentResolver.openOutputStream(insert);
            if (openOutputStream == null) {
                throw new IOException("无法打开输出流。");
            } else if (bitmap.compress(compressFormat, 90, openOutputStream)) {
                openOutputStream.close();
                return insert;
            } else {
                throw new IOException("无法保存位图。");
            }
        } catch (IOException unused) {
            contentResolver.delete(insert, null, null);
            return insert;
        } catch (Throwable th) {
            th.addSuppressed(th);
        }
    }
    return null;
}
英文:

Check if your app can function without requiring all files access permission or not. Cause if you're using that permission for manipulating media files then you may not need it.

Try replacing it with SAF (Storage Access Framework) or Mediastore API. So you won't need the All files access permission at all.

According to your use case, if you want to save a media files in external storage then you can do it using mediastore easily.

You can save bitmap to storage and update the MediaStore like this,

public static Uri saveBitmap(Context context,
                             Bitmap bitmap,
                             Bitmap.CompressFormat compressFormat,
                             String mime_type,
                             String display_name,
                             String path) {
    OutputStream openOutputStream;
    ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, display_name);
    contentValues.put(MediaStore.Images.ImageColumns.MIME_TYPE, mime_type);
    contentValues.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, path);
    ContentResolver contentResolver = context.getContentResolver();
    Uri insert = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
    if (insert != null) {
        try {
            openOutputStream = contentResolver.openOutputStream(insert);
            if (openOutputStream == null) {
                throw new IOException("Failed to open output stream.");
            } else if (bitmap.compress(compressFormat, 90, openOutputStream)) {
                openOutputStream.close();
                return insert;
            } else {
                throw new IOException("Failed to save bitmap.");
            }
        } catch (IOException unused) {
            contentResolver.delete(insert, null, null);
            return insert;
        } catch (Throwable th) {
            th.addSuppressed(th);
        }
    }
    return null;
}

答案2

得分: 0

为了将媒体内容保存到手机上,您不需要请求任何与存储相关的权限。您可以使用 MediaStore 来实现此目的:
https://developer.android.com/training/data-storage/shared/media

您还可以使用以下 Codelab(第五步)来查看具体使用 CameraX 的示例:
https://developer.android.com/codelabs/camerax-getting-started#4

英文:

In order to take and save media to the phone, you should not request any storage-related permissions. You can use MediaStore to achieve this:
https://developer.android.com/training/data-storage/shared/media

You can also use the following Codelab (and the 5th step) to see an example specifically using CameraX:
https://developer.android.com/codelabs/camerax-getting-started#4

huangapple
  • 本文由 发表于 2023年8月10日 12:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定