I am taking External Sd Card's PersistableUriPermission and storing it for further use. Now I want that when user provides me with the file path, from list of files in my application, I want to edit the document and rename it.
So I have the file path of the file to edit.
My question is how do I get that file's Uri from my TreeUri so as edit file.
Access Sd-Card's files
Use
DOCUMENT_TREE
dialog to get sd-card'sUri
.Inform user about how to choose
sd-card
on the dialog. (with pictures or gif animations)On
onActivityResult
you'll have the selected directoryUri
. (sdCardUri)Now must check if the user,
a. selected the sd-card
b. selected the sd-card that our file is on (some devices could have multiple sd-cards).
We check both a and b by finding the file through the hierarchy, from sd root to our file. If file found, both of a and b conditions are acquired.
Note:
You must provide access permission to the external storage inside your manifest and for os>=Marshmallow inside the app. https://stackoverflow.com/a/32175771/2123400
Edit Sd-Card's Files
For editing an existing image on your sd-card you don't need any of above steps if you want to invoke another app to do it for you.
Here we invoke all the activities (from all the installed apps) with the capability of editing the images. (Programmers mark their apps in the manifest for its capabilities to provide accessibility from other apps (activities)).
on your editButton click event:
and this is how to get mimeType:
Note:
On Android KitKat(4.4) don't ask the user to select the sd-card because on this version of Android
DocumentProvider
is not applicable, hence we have no chance to have access to the sd-card with this approach. Look at the API level for theDocumentProvider
https://developer.android.com/reference/android/provider/DocumentsProvider.htmlI couldn't find anything that works on Android KitKat(4.4). If you found anything useful with KitKat please share with us.
On versions below the KitKat access to sd-card is already provided by the OS.