Up until Android Pie I always stored files which the app needed to get stored on /sdcard/my-app/
, which I got via
File fBaseDir = new File(Environment.getExternalStorageDirectory(), "my-app");
One of my App stores hundreds (up to multiple thousands) of favicons in /sdcard/my-app/favicons
, and when the user creates a backup of the app, it gets stored in /sdcard/my-app/backups
. Webpage-Screenshots taken by Google Chrome when shared with my app are stored in /sdcard/my-app/screenshots
, and so on.
When I uninstall the app, I can reinstall it later and still have all the favicons, screenshots and the backups available for restoring the state and not having to re-download all the favicons (and possibly having lost all the other data).
I can also easily create a backup of the /sdcard/my-app/
directory by copying it to my PC, and restore it on another phone, for example when I migrate to a new device.
I can also use a file explorer to see which files are in that directory, and select and email them or copy them to Google Drive, or delete some of them specifically (like old backup files).
With Android 10 this approach has collapsed. I cannot add the favicons to the Images Media-Folder, because they are no real images, they would clutter the view unnecessarily and possibly ban my developer account from the Play Store for doing this. I also don't want to store all this data in an App-Private directory, because it will get deleted when the app gets uninstalled, and because other apps like an explorer cannot access them.
What are my options? Manually selecting files or directories is not an option, unless the directory only needs to be selected once and then access is granted forever (to read and write files).
I never read or write outside of /sdcard/my-app/
.
Android provides two types of physical storage locations: internal storage and external storage. On most devices, internal storage is smaller than external storage. However, internal storage is always available on all devices, making it a more reliable place to put data on which your app depends.
Android External Storage Example Code getExternalFilesDir(): It returns the path to files folder inside Android/data/data/application_package/ on the SD card. It is used to store any required files for your app (like images downloaded from web or cache files).
The data files saved over external storage devices are publicly accessible on shared external storage using USB mass storage transfer. Data files stored over external storage using a FileOutputStream object and can be read using a FileInputStream object.
In Android Q direct File access is disabled by default for the apps outside their private folders. Here few strategies you can use in your case:
requestLegacyExternalStorage
to have the old behavior but it won't work anymore with Android R, so it's really a short term solution;getExternalFilesDir()
method. It's your private folder, other apps could access these files only if they have READ_EXTERNAL_STORAGE
permission. In this case it would be good to use FileProvider
to grant access to other apps to your files.getPrimaryStorageVolume().createOpenDocumentTreeIntent()
of class StorageManager
to ask for access to the extenal primary volume. In this case you need user consent and you won't be able to use File
api directly anyway, but using DocumentFile
class you have a very similar interface, so this is the solution closer to the old behavior. It works if you need to perform operations in foreground and background, i.e. without user interaction with the exception the first interaction to request the permission.I link Flipper library for point 3, it helps to manage files like in older android versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With