11

In SDK 29 (Aka Android_Q), The access to external storage using the method Environment.getExternalStorageDirectory() is deprecated and no longer returns an accessible file.

getExtenalStoragePublicDirectory(String type)

This method was deprecated in API level 29. To improve user privacy,direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

But using Context#getExternalFilesDir(String) returns a folder inside your App-Data folder. Which is deleted on your app's uninstallation or data clearing.

But consider how app likes WhatsApp or Facebook Messenger save their backups and downloads, they use external storage and create their custom folders to store their data.

How will such apps migrate? Is there a new way?

My app is a notes application that backs-up all the notes into a folder for later restoration after app deletion or data clearing. In SDK 29, I can no longer follow such a method.

2 Answers 2

3

You can use the flag android:requestLegacyExternalStorage="true" in your manifest file.

This attribute is "false" by default on apps targeting Android 10 or higher.

However, in future versions of Android, this permission will not be available.

For next Android versions we should use scoped storage. More info here

2

In SDK 29, I can no longer follow such a method.

Use ACTION_OPEN_DOCUMENT_TREE to allow the user to pick a document tree, which could be a filesystem directory or something else. Then, use DocumentFile.fromTreeUri() to create a DocumentFile for that tree and use that to be able to write to documents to be stored inside of that tree.

How will such apps migrate?

ACTION_OPEN_DOCUMENT and ACTION_OPEN_DOCUMENT_TREE will be the two most likely on-device options. In the case of WhatsApp and Facebook, they might just elect to store the backups on their servers.

3
  • Yes, but ACTION_OPEN_DOCUMENT_TREE leaves the choice to the user. Which isn't a good choice to always let the user decide where his data will be stored. Also makes it hard to re-find that folder. I guess it's one way, but I will keep looking. Jul 14, 2019 at 13:10
  • @AhmadUCG: "but ACTION_OPEN_DOCUMENT_TREE leaves the choice to the user" -- it is the user's device, not yours. And, it is the user's data, not yours. "but I will keep looking" -- I suspect that you will be disappointed in your results, since the focus of the API Level 29 changes is to put more control in the user's hands. Jul 14, 2019 at 13:35
  • 2
    I feel that this is just another shot in the war on external storage / cheap sdcards. Now our internal memory will fill up with memes :(
    – Dr Deo
    Oct 16, 2019 at 13:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.