Writing many files on Android 11

自闭症网瘾萝莉.ら 提交于 2021-02-07 10:51:39

问题


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:

  1. Create a new file (which creates launches activity the user interacts with to save one file only), described here
  2. Grant access to a directory's contents (which allows read access but no way to write any documents), described here.
  3. 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.
  4. 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 get FileNotFoundException, 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?


回答1:


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`

The working, but deprecated method:

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)

The MediaStore way:

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.




回答2:


On Android 11 you can create a subdirectory in public directory Documents using the standard File classes as you could in below 10. And also in 10 if you knew how. Then create your files in that subdirectory. No fuss.

You have write access to a lot of other public directories too.

You do not need all files access for this.

The files you create in this way are accessable by the user using the official Files app on the device.



来源:https://stackoverflow.com/questions/64720379/writing-many-files-on-android-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!