简体   繁体   English

DocumentsContract.moveDocument() 需要的“权限”是什么?

[英]What is the "authority" that DocumentsContract.moveDocument() needs?

I have a simple file inside either the apps getExternalFilesDir or a user selected folder.我在应用程序getExternalFilesDir或用户选择的文件夹中有一个简单的文件。 When i create a subfolder and try to move the file from the parent folder into that newly created subfolder moveDocument() fails.当我创建一个子文件夹并尝试将文件从父文件夹移动到新创建的子文件夹时 moveDocument() 失败。

Logcat says Logcat 说

W/DocumentsContract: Failed to move document
java.lang.IllegalArgumentException: Unknown authority 
    at android.content.ContentResolver.call(ContentResolver.java:2412)
    at android.provider.DocumentsContract.moveDocument(DocumentsContract.java:1520)

Both DocumentFiles give an empty string when i try .getUri().getAuthority()当我尝试.getUri().getAuthority()时,两个 DocumentFiles 都给出一个空字符串

// file and subfolder are under the same parent
DocumentsContract.moveDocument(context.getContentResolver(),
                                    file.getUri(),
                                    subfolder.getParentFile().getUri(),
                                    subfolder.getUri());

Both DocumentFiles exist, i even create files inside that subfolder and that works fine, but i need to move this one from the parent into the sub.两个 DocumentFiles 都存在,我什至在该子文件夹中创建文件并且工作正常,但我需要将这个文件从父文件夹移到子文件夹中。

edit:编辑:

// if user selected
DocumentFile dir       = DocumentFile.fromTreeUri(context, persistedUri);
// if 'internal'
DocumentFile dir       = DocumentFile.fromFile(getContext().getExternalFilesDir(null));
DocumentFile subfolder = dir.createDirectory(name);
DocumentFile file      = dir.createFile("video/mp4", vidname);

// Uris internal
file:///storage/emulated/0/Android/data/com.foo.bar/files/
file:///storage/emulated/0/Android/data/com.foo.bar/files/vid
file:///storage/emulated/0/Android/data/com.foo.bar/files/1656602728866.mp4

All Uri values passed to moveDocument() have to be "document" Uri values, either obtained directly from the Storage Access Framework or derived from other Uri values that were (eg, a particular document in a tree obtained by ACTION_OPEN_DOCUMENT_TREE / ActivityResultContracts.OpenDocumentTree ).传递给moveDocument()的所有Uri值都必须是“文档” Uri值,可以直接从存储访问框架获得,也可以从其他Uri值派生而来(例如,通过ACTION_OPEN_DOCUMENT_TREE / ActivityResultContracts.OpenDocumentTree获得的树中的特定文档) . In particular, moveDocument() only works within a single provider, and only if FLAG_SUPPORTS_MOVE is included .特别是, moveDocument()仅在单个提供程序中有效,并且仅在包含FLAG_SUPPORTS_MOVE时才有效

file: Uri values, whether created directly or via DocumentFile , are ineligible. file: Uri值,无论是直接创建还是通过DocumentFile创建,都是不合格的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM