androidstorage-access-frameworkdocumentfile

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

Solution

  • 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 (e.g., 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.