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.