requestLegacyExternalStorage is not working in Android 11 - API 30

56,983

Solution 1

But now when I targetSdkVersion 30, this no longer seems to work

That is correct. Android 11 (API 30+) requestLegacyExternalStorage=true does nothing and you can no longer "opt-out". It was available in Android 10 to give developers a transition/grace period to be able to migrate to the scoped storage model.

Option 1: Migrate data in your app whilst still targeting API 29, then once you're migrated data is compatible with scoped storage you should be able to release an update targetting API 30 - https://developer.android.com/training/data-storage/use-cases

This can come with its own problems if users skip this version and updates directly from a previous version to the latest and you're stuck with un-migrated data you can't access.

Option 2: It seems that Google sees this obvious caveat and has included a preserveLegacyExternalStorage=true option when targetting API 30 to allow you to migrate data. https://developer.android.com/reference/android/R.attr#preserveLegacyExternalStorage

Going forward you can reference this table for deciding what storage "framework" to use based on the use-case: https://developer.android.com/training/data-storage

There is a potential that some apps simply won't be able to successfully migrate, based on how they interacted with the File API as Google's solution will not encompass all current use-cases and there might not be a migration path.

For instance, I released an app a couple of years ago that allowed users to update album artwork using MediaStore and ContentResolver to update the data for the album artwork image - this was stored in shared storage. Having looked at the Android 10+ AOSP MediaProvider source code it seems that apps that used to use MediaStore to update album artwork to point to a data file no longer works, simply because the MediaProvider internally creates its own artwork in a hidden .thumbnails folder looking directly at the mp3's and using a MediaExtractor, and never references the ContentValues that were inserted to reference the artwork. So even though you can update the artwork yourself, query the MediaStore for it and see it, other apps have to use ContentResolver#loadThumbnail in API 29+ that does not reference your updated values and either creates an artwork lazily, or picks the already generated file in the .thumbnails folder. Obviously, none of this is documented, and I got a massive backlash to my app with negative reviews, yet these changes were breaking changes and completely out of my control and took me looking through AOSP source code to find that Android had fundamentally changed behaviour.

(This wasn't a rant, but an example of how these changes offered no migration path because of fundamental undocumented behaviour to AOSP).

Solution 2

As stated in https://developer.android.com/about/versions/11/privacy/storage there are some changes regarding storage on Android 11:

  • Android 10 devices
    • requestLegacyExternalStorage will continue to work regardless of target sdk
  • Android 11 devices
    • new installation targetting sdk 29: requestLegacyExternalStorage value is respected
    • new installation targetting sdk 30: requestLegacyExternalStorage is always false
    • upgrade from targetting sdk 29 to 30: if preserveLegacyExternalStorage is set then requestLegacyExternalStorage is true (this is pure migration case and this state won't be preserved should user uninstall/reinstall the app)

You're pretty much forced to implement scoped storage at this point. Unless you're ready to migrate just keep targetting sdk 29 as there's no way to enforce legacy storage on Android 11 devices with target sdk 30.

update: play store requires target sdk 30 as of August 2021

Solution 3

Don't do this until early 2021 (said by google):-

If you want to target to android 11, you should use the MANAGE_EXTERNAL_STORAGE permission.

Visit this page for more details: https://developer.android.com/training/data-storage/manage-all-files

Solution 4

Applications that run on Android 11 but target Android 10 (API level 29) can still request the requestLegacyExternalStorage attribute. This flag allows applications 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 updating application to target Android 11, the system ignores the requestLegacyExternalStorage flag.

Share:
56,983
Ebin Joy
Author by

Ebin Joy

Updated on July 09, 2022

Comments

  • Ebin Joy
    Ebin Joy almost 2 years

    Google has introduced some changes recently related to storage APIs in API 29 like scoped storage and we opted out by adding 'requestLegacyExternalStorage=true' in Manifest. But now when I targetSdkVersion 30, this no longer seems to work. Some of the files in the download directories were not listing (File.listFiles) after this change.

  • Mark
    Mark over 3 years
    This comes with a big caveat, the app must conform to the Google Play policy for this permission, " the app’s usage of the permission must fall within permitted uses, and must be directly tied to the core functionality of the app" - so unless your app is a File Manager or Virus scanning app etc.. I'd advise not to use this permission, otherwise you might find its rejected and have a load of refactoring to do.
  • Mark Delphi
    Mark Delphi over 3 years
    What about the Android versions below SKD 29? How to deal with permissions? My App supports SDKs 21+. I still need to consider permissions for those versions. Thanks!
  • Moustafa EL-Saghier
    Moustafa EL-Saghier over 3 years
    any tutorial about step for doing that, please?
  • Noor Hossain
    Noor Hossain over 3 years
    I have searched for 6 hours, but at last found the clear concept, thanks, But, is there any tutorial that show migrating and supports all api regardless ?
  • opLW
    opLW over 3 years
    Did you test the "upgrade from targetting sdk 29 to 30" case. I have tried several time. But accessing to legacy storage is always failed whether target from 28 to 30 or from 29 to 30.
  • Pawel
    Pawel over 3 years
    @opLW I have no issue with installation upgrade on API 30 emulator.
  • opLW
    opLW over 3 years
    Oh, sorry. I miss something. Now I get the case right.
  • user3066829
    user3066829 about 3 years
    I have application targeting 29 in playstore. Do i need to release new version immediately on playstore befor 5th may? Or i can wait for my next release?
  • Mark
    Mark about 3 years
    No - you only need to do this if you target 30 - "This is only applicable to apps that target Android 11 (API level 30) and declare the MANAGE_EXTERNAL_STORAGE permission, which is added in Android 11" - support.google.com/googleplay/android-developer/answer/10467‌​955 - no doubt this will be a temporary reprieve and Google might decide somepoint in the future all new apps and releases target 30+. Basically you should have a migration strategy in place when you target api 30 .. I'd be surprised, unless you're a big player in the app sphere that you'll be an easy pass for the permission.
  • Mark
    Mark about 3 years
    "update: play store will require target sdk 30 as of August 2021" - that refactor you've been putting off just got a deadline ...
  • Mark
    Mark about 3 years
    see below update from @Pawel - "update: play store will require target sdk 30 as of August 2021"
  • Dr.jacky
    Dr.jacky about 3 years
    @Pawel I migrated my code to the new solution for Android API 30 and it works on Emulator API 30 but still, picking an image from the gallery doesn't work on Emulator API 29!; The targetSDK is 30; But, if I add requestLegacyExternalStorage = true while I'm following that new solution (MediaStore - API 30), it works on every API version. Is that what should we do?
  • Dr.jacky
    Dr.jacky about 3 years
    @MarkKeen Would you please take a look into: stackoverflow.com/questions/63364476/…
  • Pawel
    Pawel about 3 years
    @Dr.jacky sorry but I can't advocate for google on how they're going to execute their store policies. If you have issues with scoped storage implementation open up a new question.
  • ahmetvefa53
    ahmetvefa53 over 2 years
    @Pawel on my app preserveLegacyExternalStorage = true . ı have installed on android device after that update system to android 11 but externalstorageLegacy is false why?
  • RkKhanpuriya
    RkKhanpuriya over 2 years
    @MarkKeen any solution for this problem?
  • Jianwu Chen
    Jianwu Chen over 2 years
    For my case, run Android 11 is good with permission of:MANAGE_EXTERNAL_STORAGE, but when run on Android 10 or bellow, I can list the media files such as jpg file, but no other files will be shown? I'm completely blocked.
  • Jianwu Chen
    Jianwu Chen over 2 years
    I applied for this permission. And get approved. my application is an eBook reader. But the problem is now my app only works for android 11+. For the older version, there's no way to get MANAGE_EXTERNAL_STORAGE permission.