如何获得MANAGE_EXTERNAL_STORAGE权限

问题描述

我正在尝试在我的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" />

MainActivity:

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.

但是我如何获得该许可?

解决方法

Android 10 不需要“android.permission.MANAGE_EXTERNAL_STORAGE”,manifest 中应用程序标签下的 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:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...