close

[Solved] requestLegacyExternalStorage is not working in Android 11 – API 30

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error requestLegacyExternalStorage is not working in Android 11 – API 30 in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How requestLegacyExternalStorage is not working in Android 11 – API 30 Error Occurs?

Today I am facing the following error requestLegacyExternalStorage is not working in Android 11 – API 30 in Java.

How To Solve requestLegacyExternalStorage is not working in Android 11 – API 30 Error ?

  1. How To Solve requestLegacyExternalStorage is not working in Android 11 – API 30 Error ?

    To Solve requestLegacyExternalStorage is not working in Android 11 – API 30 Error 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.

  2. requestLegacyExternalStorage is not working in Android 11 – API 30

    To Solve requestLegacyExternalStorage is not working in Android 11 – API 30 Error 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.

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

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. 

Going forward you can reference this table for deciding what storage “framework” to use based on the use-case:

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read