问题描述
迁移到Android Q后,我再也找不到合适的方法来获取对documents文件夹(/storage/emulated/0/Documents
)的写访问权
After migrating to Android Q I can no longer find a suitable way to gain write access to the documents folder (/storage/emulated/0/Documents
)
在任何人提到范围存储的其他问题之前,我已经阅读了其中的很多问题,到目前为止,从我所看到的所有解决方案中,所有解决方案都使用应用程序特定目录或仅访问媒体目录(而非文档文件夹)访问).
And before anyone mentions the many other questions on scoped storage I've read through many of them and from what I've seen so far all of the solutions use a app specific directory or accessing a media directory only (not documents folder access).
根据我对Android Q的了解,我可以选择:
- 使用只能由我的应用程序(或我允许的应用程序)访问的应用程序特定目录
- 允许用户选择要将文件存储在哪里(我认为这可以是公共位置)
ACTION_OPEN_DOCUMENT_TREE
我开发的应用程序用于对主题进行测试,测试数据自动存储在可公开访问的Documents文件夹中,如下所示:/storage/emulated/0/Documents/myAppName/subjectName/testData-todaysDate.pdf
The applications I develop are used to conduct tests on subjects, the data from the tests automatically gets stored in the publicly accessible Documents folder somewhat like: /storage/emulated/0/Documents/myAppName/subjectName/testData-todaysDate.pdf
当用户想要访问测试数据时,他们将智能手机插入计算机并导航到documents文件夹,其余的工作非常明显.因此,我必须使用可公开访问的存储.还要想象他们想通过另一个应用程序"Same deal"在手机上打开测试数据!
When the user wants to access the test data they plug their smartphone into a computer and navigate to the documents folder and the rest is pretty obvious. For this reason I have to use publicly accessible storage. Also imagine they want to open the test data on their phone via another app, Same deal!
因此,我正在寻找的解决方案需要能够执行以下操作:
So the solution I'm looking for needs to be able to do the following:
- 不多次请求许可(一次可以)
- 自动保存测试数据而无需用户提示(因为将进行数百次测试)
- 保存到公共文档文件夹,例如
/storage/emulated/0/Documents/
- 不涉及用户选择目录,因此不涉及
ACTION_OPEN_DOCUMENT_TREE
- 理想情况下,数据是持久的,因此卸载应用程序不会导致数据丢失
- Doesn't ask for permission multiple times (once is ok)
- Automatically saves test data without user prompt (as hundreds of tests will be conducted)
- Saves to public documents folder e.g.
/storage/emulated/0/Documents/
- Doesn't involve the user selecting the directory so NOT
ACTION_OPEN_DOCUMENT_TREE
- Ideally the data is persistent so uninstalling the app doesn't cause data lose
我了解到,Android Q中的这些更改旨在使用户更了解应用程序访问数据的方式,但是一旦他们了解了您的应用程序如何使用数据,就不会有问题了.
I get that these changes in Android Q are to bring users more "into the loop" with how apps are accessing their data but once they understand how your app is using your data there shouldn't be a problem.
推荐答案
仅适用于Android Q.
For Android Q only.
String collection = "content://media/1c11-2404/file"; // One can even write to micro SD card
String relative_path = "Documents/MyFolder";
Uri collectionUri = Uri.parse(collection);
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
contentValues.put(MediaStore.MediaColumns.SIZE, filesize);
contentValues.put(MediaStore.MediaColumns.DATE_MODIFIED, modified );
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relative_path);
contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1)
Uri fileUri = context.getContentResolver().insert(collectionUri, contentValues);
具有uri人可以打开输出流以写入文件内容.
Having an uri one can open an output stream to write the content for the file.
这篇关于将文件写入Android Q中的可公开访问的文档文件夹-范围存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!