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
How to Monitor Sim State Change
Horizontalscrollview: Auto-Scroll to End When New Views Are Added
Send Post Request with Params Using Retrofit
How to Share Image + Text Together Using Action_Send in Android
How Could I Filter the Listview Using Baseadapter
Mediarecorder Start Failed: -38
No Adapter Attached; Skipping Layout
Are Parameters in Strings.Xml Possible
How to Display More Than 3- Levels of Expandable List View
Sharedpreferences and Thread Safety
Android: Setselection Having No Effect on Spinner
Android: Fragments Overlapping Issue
Firebase Onmessagereceived Not Called When App Is in the Background