如何获取MANAGE_EXTERNAL_STORAGE权限

23

我正在尝试在我的Android 10 (API 29)设备上获取MANAGE_EXTERNAL_STORAGE权限。

https://developer.android.com/training/data-storage/manage-all-files

清单文件:

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

主活动:

Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
startActivity(intent);

结果是:

No Activity found to handle Intent { act=android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION }

好的,这种行为是可能的。文档上说:

In some cases, a matching Activity may not exist, so ensure you safeguard against this.

但是我该如何获得那个许可呢?


2
该权限及其相关操作仅存在于API 30上,对于API 29设备则不适用。你想做什么? - ianhanniballake
@ianhanniballake 需要访问所有文件。 - Serhii K.
3
https://dev59.com/JlIG5IYBdhLWcg3wlibO - blackapps
https://dev59.com/BVIG5IYBdhLWcg3w01A9 - Simon
1个回答

42

Android 10不需要“android.permission.MANAGE_EXTERNAL_STORAGE”,在清单文件中的应用程序标签下使用android:requestLegacyExternalStorage="true"

对于Android 11,请尝试以下方法。

if (Environment.isExternalStorageManager()) {
    //todo when permission is granted
} else {
    //request for the permission
    Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
    Uri uri = Uri.fromParts("package", getPackageName(), null);
    intent.setData(uri);
    startActivity(intent);
}

1
如果用户在不启用它的情况下从设置中返回,该怎么办?您有处理请求代码的吗? - nayan dhabarde
1
Scoped storage enforcement 在 Android 11 上运行但针对 Android 10(API 级别 29)的应用程序仍然可以请求 requestLegacyExternalStorage 属性。此标志允许应用程序暂时退出与作用域存储相关的更改,例如授予对不同目录和不同类型媒体文件的访问权限。将您的应用程序更新为针对 Android 11 后,系统将忽略 requestLegacyExternalStorage 标志。 - Nahid Hasan
1
这应该是正确的答案,干得好。 - irkoch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接