Android Saf (Storage Access Framework): Get Particular File Uri from Treeuri

Storage Access Framework Getting Correct Uri Path Delete/Edit/Get File

Uri.fromFile() and DocumentFile.fromTreeUri() create uris from two different worlds.

While they currently look very similar, this is just coincidence and could change with any future Android release.

There is no "non-hacky" way to convert from one to the other. If you want a dirty solution, you can go for reflection (view the source code of DocumentFile.fromTreeUri and possibly use the Storage class on newer Android versions.

Also see:
Android - Storage Access Framework - Uri into local file

Convert file PATH to TreeUri (Storage Access Framework)

To convert file path to uri use this:-

DocumentFile fileuri = DocumentFile.fromFile(new File(filepath));

Then you can perform delete,rename operations on this fileuri.

Android SAF: Uri of a folder (on disk) from DocumentsContract.getTreeDocumentId has different form, in respect to the previously authorized one

Offtopic: you don't need intent.addFlags() in 'openPickerForFolderSelection' method.

Now on your topic - I think you just need to use DocumentContract.buildDocumentUriUsingTree() to get the uri you need (atleast this worked for me when I tried to create file/directory this way), so it would be something like:

Uri oldParentUri = Uri.parse(parentFolderUriString);
String id = DocumentsContract.getTreeDocumentId(oldParentUri );
Uri parentFolderUri= DocumentsContract.buildChildDocumentsUriUsingTree(oldParentUri , id);

On the other hand I suggest you use DocumentFile API - it's much easier imo. your 'createFolderInFolder' would then look like this:

Uri parentFolderUri = Uri.parse(parentFolderUriString);
DocumentFile parentFolder = DocumentFile.fromTreeUri(context, parentFolderUri);
parentFolder.createDirectory(folderName);

Then you can easily get the created folder uri and do something from there.



Related Topics



Leave a reply



Submit