22

I'm trying to get the MANAGE_EXTERNAL_STORAGE permission on my Android 10 (API 29) device.

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

Manifest:

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

MainActivity:

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

The result is:

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

Okay, this behaviour is possible. The doc says:

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

But how can I obtain that permission?

Serhii K.
  • 639
  • 1
  • 8
  • 17

1 Answers1

41

Android 10 doesn't require "android.permission.MANAGE_EXTERNAL_STORAGE", android:requestLegacyExternalStorage="true" under application tag in manifest will work.

For android 11, try this

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);
}
jox
  • 2,218
  • 22
  • 32
iamrohitsaini
  • 511
  • 4
  • 4
  • 1
    what if the user returns from settings without enabling it? Do you have the request code to handle that? – nayan dhabarde Apr 29 '21 at 13:11
  • 1
    Scoped storage enforcement Apps that run on Android 11 but target Android 10 (API level 29) can still request the requestLegacyExternalStorage attribute. This flag allows apps to temporarily opt out of the changes associated with scoped storage, such as granting access to different directories and different types of media files. After you update your app to target Android 11, the system ignores the requestLegacyExternalStorage flag. – Nahid Hasan May 08 '21 at 06:19
  • 1
    this should be the accepted answer, good job – irkoch Oct 25 '21 at 13:30