ADVERTISEMENT

requestLegacyExternalStorage is not working in Android 11 - API 30

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 trasition/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 compatiable with scoped storage you should be able to release an update targetting API 30 - https://developer.android.com/preview/privacy/storage#scoped-storage.

This can come with its own problems if users skip this version and updates dirctly 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 usecase : 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 usecases and there might not be a migration path.

For instance I relased an app a couple 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 sorage. 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 it's 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 a 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 exmaple of how these changes offered no migration path because of fundemental undocumented behaviour to AOSP).


As stated in https://developer.android.com/preview/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.