Chris S
Chris S

Reputation: 93

Using legacy storage in Android API29

I am trying to use "Legacy Storage" for my app so that it can run on API29, as a temporary measure until I can understand the new storage model. I have set

requestLegacyExternalStorage="true"

in the manifest, but it still doesn't work. The Android docs say:

Test scoped storage

To enable scoped storage in your app, regardless of your app's target SDK version and manifest flag values, enable the following app compatibility flags:

DEFAULT_SCOPED_STORAGE (enabled for all apps by default) FORCE_ENABLE_SCOPED_STORAGE (disabled for all apps by default)

To disable scoped storage and use the legacy storage model instead, unset both flags.

How do I unset the flags? Any other advice?

Upvotes: 3

Views: 6094

Answers (2)

Kamil Komnacki
Kamil Komnacki

Reputation: 456

Back to the main question:

How do I unset the flags? Any other advice?

The flags you mention (DEFAULT_SCOPED_STORAGE and FORCE_ENABLE_SCOED_STORAGE) are for development purpouse only. You can set them in App Compatibility Changes panel on settings. To find this pannel, you have to unlock Developer options in settings and you can find there App Compatibility Changes.

For more information, see the documentation: How to identify which changes are enabled

Upvotes: 3

Chris S
Chris S

Reputation: 93

At last (with more research on stackoverflow!) I have found the way around my problem, so I'll describe it here in case it is of help to anyone else.

THE APP WORKS OK UP TO API28:

My app downloads a file called myfile.txt and saves it in external memory, using DownloadManager. The directory where DownloadManager saves it is determined by

request.setDestinationInExternalPublicDir("","myfile.txt");

which returns the directory path/storage/emulated/0

When I read the file, I get the directory to read from using

String myPath = Environment.getExternalStorageDirectory();

which returns the same directory path. This works OK up to API28.

THE PROBLEM:

For API29, I put

android:requestLegacyExternalStorage="true"

in the <application....../> section of the manifest, but even then request.setDestinationInExternalPublicDir doesn't work - it crashes with a runtime error.

THE ANSWER THAT WORKED FOR ME:

Instead of request.setDestinationInExternalPublicDir I used

request.setDestinationInExternalFilesDir(this,"","myfile,txt");

which works OK in API29 and also the earlier versions. However, it returns a different directory path: /storage/emulated/0/Android/data/com.barney.aboutmyjourney/files (com.barney/aboutmyjourney is my app).

To get the directory path when reading the file, I therefore used

File myFile=this.getExternalFilesDir("myfile.txt");
String myPath=myFile.getParent();

which returns the path I want (/storage/emulated/0/Android/data/com.barney.aboutmyjourney/files).

This now works for me. Now just lots of slog modifying all my file IO. Perhaps I should just understand and used Scoped Storage instead!

Upvotes: 0

Related Questions