0

The application I'm currently working on requires a manual setup (entering some information) on device provisioning. This information needs to be written to a file that should not be deleted when the application is uninstall or the application data is wiped (user support requirement, as they can direct users to do this in some cases)

There was a very similar old question, but the answer is now deprecated and no up-to-date answer has been posted

Keep files after uninstallation of android app

So the question is, given the deprecation of Environment.getExternalStorageDirectory() on Android 10, how do we programmatically write/read a file that will not be deleted when the application is uninstalled or the data is wiped?

For what is worth, we can not rely on app auto backup, as the users don't have google accounts configured.

Thanks

Robert Estivill
  • 12,369
  • 8
  • 43
  • 64
  • Use SAF to let the user pick a suitable directory. – blackapps Sep 28 '20 at 18:24
  • Although there's user interaction as part of the device setup, this need to be programmatically done, as stated in the question. – Robert Estivill Sep 28 '20 at 18:46
  • Just request legacy external storage in manifest and you can use external storage as usual. – blackapps Sep 28 '20 at 19:20
  • That is not correct. The documentation clearly states: `When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps.` https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory() – Robert Estivill Sep 28 '20 at 20:15
  • Indeed. But repeat: if you request legacy external storage in manifest it is. – blackapps Sep 28 '20 at 20:20
  • True. That would work. I should have said that I'm already targeting android 11, in which requestLegacyStorage is ignored sadly. :( https://developer.android.com/about/versions/11/privacy/storage – Robert Estivill Sep 29 '20 at 07:39
  • Yes and no. It will not have effect on Android 11 devices but it continous to work on Android 10 devices. And you will not need it that much on Android 11 devices as it is not that restrictive any more. – blackapps Sep 29 '20 at 07:42
  • Hm. What do you mean Android 11 is "not that restrictive" ? I think it is, as not even request legacy mode will work in Android 11, right? – Robert Estivill Sep 29 '20 at 07:47
  • . In Android 11 devices every app can directly read and write to folders like Download, Documents, DCIM, Pictures, Alarms, Movies and so on. Google did a step back. – blackapps Sep 29 '20 at 07:48

1 Answers1

1

To summarize while targetting 30.

For Android 10 device: Request legacy external storage to get external storage access as usual.

And Googles step back for Android 11 devices: use directories like Download, Pictures, Movies, Documents, DCIM and so on. Read and write access for all. Android OS is very picky to use the right extensions for files to be created in those folders.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • Can you point me to any reference, documentation or sample backing up that statement? I only seem to find more restrictions on Android 11, or different API's to accomplish the same thing MediaStore.* – Robert Estivill Sep 29 '20 at 09:08
  • Can you first confirm that those directories are writable? I would think you will have tried by now. – blackapps Sep 29 '20 at 09:23
  • Sorry for the delay, was able to try this out and indeed work in Android 10 and 11. I added the code to the answer. The weird part is that this worked without requesting the legacy external storage. While I'm suspicious about my testing on Android 10 because the device is running some enterprise management software, it totally works on android 9 and 11 in which I tested in personal devices. – Robert Estivill Oct 01 '20 at 13:09
  • Further there is no reference as they are just my experiences. – blackapps Oct 01 '20 at 14:21