Scenario
I am trying to create many files, as a feature for the user. For example, I might write an app which creates one file for each song they listened to for the past few weeks (e.g. a text file with lyrics). I can't make the user pick the directory and filename for each file I generate, it would take them hours. The user should have access to these documents from other apps.
Solutions (not really)
In Android 11, it seems like Storage Access Framework is not going to be useful. I noticed there were 2 initially interesting options:
- Create a new file (which creates launches activity the user interacts with to save one file only), described here
- Grant access to a directory's contents (which allows read access but no way to write any documents), described here.
- Note: A solution already exists if you target Android 10 and request
WRITE_EXTERNAL_STORAGE
. Unfortunately this permission is denied on Android 11. Therefore, I cannot use the solution posted here. - Get access to the directory using Storage Access Framework (specifically
ACTION_OPEN_DOCUMENT_TREE
) to get access to a directory, but I couldn't write files into this directory. (I getFileNotFoundException
, as this is not a normal path, but a tree path.)
Verdict
I guess in my case, I need to go down the route of "manage all files on a storage device", as described here which involves launching an extra activity with ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
. I'd rather not ask permission to read the entire drive or make the user go into the settings app to give permission. Writing files shouldn't require any permission...
How would you handle saving many (non-media) files for the user?