1

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
NikkyD
  • 2,209
  • 1
  • 16
  • 31
  • How did you create the `DocumentFile` objects? What do their `getUri()` values look like? – CommonsWare Jun 29 '22 at 18:38
  • For example, "I have a simple file inside either the apps getExternalFilesDir" is not going to work with `moveDocument()`. Both the source and destinations need to be part of the Storage Access Framework, not filesystem locations, HTTPS URLs, or anything else. – CommonsWare Jun 29 '22 at 19:47
  • edited/added the info – NikkyD Jun 30 '22 at 15:34

1 Answers1

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So there is no way to move (not copy) a DocumentFile when using the new API with the 'internal' folders ? – NikkyD Jun 30 '22 at 17:47
  • 1
    @NikkyD: There is no single-function solution in the Android SDK. You would need to initially copy the content (open both streams, copy the bytes, close both streams), then delete the original if the copy succeeded. – CommonsWare Jun 30 '22 at 17:52