1

I want my app to write a text file to the Documents folder (or any other part of the memory that persists after the app is uninstalled).

Up to Android 9 (SDK 28) the Documents folder could be accessed as follows

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);

which is now deprecated due to the new scoped memory policy. The official documentation says

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.

The fist option, Context#getExternalFilesDir(String) is not what I want because

This is like getFilesDir() in that these files will be deleted when the application is uninstalled

The third option, Intent#ACTION_OPEN_DOCUMENT is not sufficient because I want to write the file before accessing it.

Looks like MediaStore is the only way. I have seen some incomplete code examples with MediaStore, but I could not find an exhaustive answer.

Please, could you provide a complete Java fragment that shows how to write/read a text file from uninstallation-persistent memory in Android 10? Despite the strict privacy policies of Android 10, I don't see why this should not be possible, provided the user grants permissions. Thanks a lot.

MathMax
  • 571
  • 7
  • 22
  • "I want to write the file before accessing it" -- `ACTION_OPEN_DOCUMENT` is for reading in an existing piece of content. `ACTION_CREATE_DOCUMENT` is for creating a new piece of content, which is your use case. "Looks like MediaStore is the only way" -- `MediaStore` is primarily for images, videos, and audio files. A text file is none of those. You *could* consider it to be a "download" and use `MediaStore.Downloads`, though IMHO `ACTION_CREATE_DOCUMENT` is a better choice. – CommonsWare Feb 07 '20 at 22:14
  • @CommonsWare thank you for your comment, I was not aware of the solutions you mentioned. A potential alternative to `MediaStore.Downloads` might be `MediaStore.Files`, which the doc defines as "Media provider table containing an index of all files in the media storage, including non-media files. This should be used by applications that work with non-media file types (text, HTML, PDF, etc)". – MathMax Feb 10 '20 at 11:35

1 Answers1

1

if you want to read/write in persistent folders, e.g. "Download", without user interaction. have a look here https://stackoverflow.com/a/65314962/7037357

here https://commonsware.com/blog/2020/02/15/scoped-storage-stories-undocumented-documents.html you can find a collection of blogs exploring the "Scoped Storage" feature. In the link you can find an undocumented way to access also the "Documents" folder using the same approach.

albaspazio
  • 121
  • 1
  • 6
  • Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the external resource is unreachable or goes permanently offline. – atline Dec 16 '20 at 01:52
  • Thank you! Is it also possible for the app to access the document after it is uninstalled and reinstalled? I’ve read somewhere that with scoped storage, apps lose ownership of the external files they create once uninstalled. In the meantime, I’ll give your answer a shot and I’ll get back to you once I’ve done so. – MathMax Dec 16 '20 at 03:54
  • I have tried writing it on Android 11 which works fine, reading is not possible once after losing the ownership. Did you find any workaround without user interaction and `MANAGE_EXTERNAL_STORAGE` permission? – Varsha Kulkarni Dec 06 '21 at 12:32