11

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

但是我怎样才能获得该许可呢?

4

1 回答 1

23

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);
}
于 2021-04-06T12:49:24.377 回答