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.
In Android 11, it seems like Storage Access Framework is not going to be useful. I noticed there were 2 initially interesting options:
WRITE_EXTERNAL_STORAGE
. Unfortunately this permission is denied on Android 11. Therefore, I cannot use the solution posted here.ACTION_OPEN_DOCUMENT_TREE
) to get access to a directory, but I couldn't write files into this directory. (I get FileNotFoundException
, as this is not a normal path, but a tree path.)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?
There are 2 methods, but first, I'll define the file and directory name, which I will later save inside the external (sdcard) Downloads folder
val outputFilename = "my_file"
val outputDirectory = "my_sub_directory" // The folder within the Downloads folder, because we use `DIRECTORY_DOWNLOADS`
Although the Environment.getExternalStoragePublicDirectory
was deprecated, it still works on apps targeting and running on Android 11.
file = File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS),
"$outputDirectory/$outputFilename"
)
val outputStream = FileOutputStream(file)
Alternatively, you could use MediaStore's Files collection, suggested Gaurav Mall
here. I didn't know about the MediaStore files
collection...
I rewrote it in kotlin and modified it for writing files here:
val resolver = context.contentResolver
val values = ContentValues()
// save to a folder
values.put(MediaStore.MediaColumns.DISPLAY_NAME, outputFilename)
values.put(MediaStore.MediaColumns.MIME_TYPE, "application/my-custom-type")
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + "/" + outputDirectory)
val uri = resolver.insert(MediaStore.Files.getContentUri("external"), values)
// You can use this outputStream to write whatever file you want:
val outputStream = resolver.openOutputStream(uri!!)
// Or alternatively call other methods of ContentResolver, e.g. .openFile
In both cases below, you don't need WRITE_EXTERNAL_STORAGE
for Android 29 and above.