简体   繁体   中英

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

I have a simple file inside either the apps getExternalFilesDir or a user selected folder. When i create a subfolder and try to move the file from the parent folder into that newly created subfolder moveDocument() fails.

Logcat says

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()

// 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.

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 ). In particular, moveDocument() only works within a single provider, and only if FLAG_SUPPORTS_MOVE is included .

file: Uri values, whether created directly or via DocumentFile , are ineligible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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