Persisting files across reinstalls in app specific directory in Downloads folder

738 views Asked by At

We have an app that allows users to, among other things, download files and store them locally. To provide greater access to the files (say for example, manual backups), the files are downloaded into an app specific folder in the Download directory.

Till Android Q, the app could simply use the File API to check if the file exists locally and update the UI accordingly. However, since then we've shifted to using the Mediastore API. However, one of the problems faced by users is loss of access to the downloaded files after a reinstall. After a reinstall the app has no way of knowing if a previous install had files left over. It can never properly manage the folder.

The scoped storage documentation says SAF is the way to go give apps full access to read and write to folders. Is there another way? If we are to use SAF, how do we need to request the permission every time the user opens the app? Is there any way to persist the file schema URI that is returned? There is little documentation for this use case (either that or we didn't find any).

Thanks.

2

There are 2 answers

2
blackapps On

For Android 10 devices request legacy external storage in manifest file and you have File access to Download folder as usual.

For Android 11 devices the Download folder is readable in the old way and even writable. No need for media store.

4
Weidian Huang On

You can try to use the shared storage. It used for sharing user data that can or should be accessible to other apps and saved even if the user uninstalls your app.

To use it, you need the permission of MANAGE_EXTERNAL_STORAGE. But when you submit the app to Google Play, there are some limitations there, please read here.
Because of COVID-19 issue, apps need the MANAGE_EXTERNAL_STORAGE permission cannot be uploaded to Google Play. So for now, you can target the SDK version to 29, and use requestLegacyExternalStorage flag. After early 2021, you can try to use the shared storage.