[英]Creating a writeable DocumentFile from URI
我正在嘗試使基於File
的文檔系統適應使用DocumentFile
,以便允許在 API >= 29 上進行外部存儲讀/寫訪問。
我讓用戶使用Intent.ACTION_OPEN_DOCUMENT_TREE
選擇 SD 卡根目錄,並按預期返回Uri
,然后我可以使用以下方法處理:
getContentResolver().takePersistableUriPermission(resultData.getData(),
Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
我可以成功瀏覽外部存儲內容,直到選定的根目錄。 都好。
但是我需要做的是在選定的(子)文件夾中寫入一個任意文件,這就是我遇到問題的地方。
DocumentFile file = DocumentFile.fromSingleUri(mContext, Uri.parse(toPath));
Uri uri = file.getUri();
FileOutputStream output = mContext.getContentResolver().openOutputStream(uri);
除了openOutputStream()
調用,我得到:
java.io.FileNotFoundException: Failed to open for writing: java.io.FileNotFoundException: open failed: EISDIR (Is a directory)
這讓我有點困惑,但“找不到文件”部分表明我可能需要先創建空白輸出文件,所以我嘗試這樣做,例如:
DocumentFile file = DocumentFile.fromSingleUri(mContext, Uri.parse(toPath));
Uri uri = file.getUri();
if (file == null) {
return false;
}
if (file.exists()) {
file.delete();
}
DocumentFile.fromTreeUri(mContext, Uri.parse(getParentPath(toPath))).createFile("", uri.getLastPathSegment());
FileOutputStream output = mContext.getContentResolver().openOutputStream(uri);
我得到一個java.io.IOException
:
java.lang.IllegalStateException: Failed to touch /mnt/media_rw/0B07-1910/Testing.tmp: java.io.IOException: Read-only file system
at android.os.Parcel.createException(Parcel.java:2079)
at android.os.Parcel.readException(Parcel.java:2039)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:658)
at android.content.ContentResolver.call(ContentResolver.java:2042)
at android.provider.DocumentsContract.createDocument(DocumentsContract.java:1327)
at androidx.documentfile.provider.TreeDocumentFile.createFile(TreeDocumentFile.java:53)
at androidx.documentfile.provider.TreeDocumentFile.createFile(TreeDocumentFile.java:45)
這對我來說沒有意義,因為樹應該是可寫的。
就其價值而言,我從Intent.ACTION_OPEN_DOCUMENT_TREE
返回的Uri
如下所示:
content://com.android.externalstorage.documents/tree/0B07-1910%3A
有趣的是,當我使用該Uri
創建一個DocumentFile
對象進行瀏覽時,使用documentFile = DocumentFile.fromTreeUri(context, uri)
,然后documentFile.getURI().toString()
看起來像:
content://com.android.externalstorage.documents/tree/0B07-1910%3A/document/0B07-1910%3A
即,它的末尾附加了一些東西。
然后,我進入應該是可寫文件夾(如“下載”),並嘗試如上所述創建可寫文件。 “下載”文件夾獲取Uri
:
content://com.android.externalstorage.documents/tree/0B07-1910%3A/document/0B07-1910%3ADownload
然后我在上面用於toPath
的Uri
是:
content://com.android.externalstorage.documents/tree/0B07-1910%3A/document/0B07-1910%3ADownload/Testing.tmp
這會導致前面描述的嘗試創建它的問題。
我實際上還沒有找到任何關於在存儲訪問框架限制下編寫任意文件的體面信息。
我究竟做錯了什么? 謝謝。 :)
Uri uri = uri obtained from ACTION_OPEN_DOCUMENT_TREE
String folderName = "questions.59189631";
DocumentFile documentDir = DocumentFile.fromTreeUri(context, uri);
DocumentFile folder = documentDir.createDirectory(folderName);
return folder.getUri();
對可寫文件使用 createFile()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.