Android SAF (Storage Access FrameWork): Get particular file Uri from TreeUri

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

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.

回答1:

Access Sd-Card's files

Use DOCUMENT_TREE dialog to get sd-card's Uri.

Inform user about how to choose sd-card on the dialog. (with pictures or gif animations)

// call for document tree dialog Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT_TREE); 

On onActivityResult you'll have the selected directory Uri. (sdCardUri)

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);     switch (requestCode) {         case REQUEST_CODE_OPEN_DOCUMENT_TREE:             if (resultCode == Activity.RESULT_OK) {                 sdCardUri = data.getData();              }              beak;      }   } 

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.

//First we get `DocumentFile` from the `TreeUri` which in our case is `sdCardUri`. DocumentFile documentFile = DocumentFile.fromTreeUri(this, sdCardUri);  //Then we split file path into array of strings. //ex: parts:{"", "storage", "extSdCard", "MyFolder", "MyFolder", "myImage.jpg"} // There is a reason for having two similar names "MyFolder" in  //my exmple file path to show you similarity in names in a path will not  //distract our hiarchy search that is provided below. String[] parts = (file.getPath()).split("\\/");  // findFile method will search documentFile for the first file  // with the expected `DisplayName`  // We skip first three items because we are already on it.(sdCardUri = /storage/extSdCard) for (int i = 3; i 

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:

String mimeType = getMimeTypeFromMediaContentUri(mediaContentUri); startActivityForResult(Intent.createChooser(new Intent(Intent.ACTION_EDIT).setDataAndType(mediaContentUri, mimeType).putExtra(Intent.EXTRA_STREAM, mediaContentUri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION), "Edit"), REQUEST_CODE_SHARE_EDIT_SET_AS_INTENT); 

and this is how to get mimeType:

public String getMimeTypeFromMediaContentUri(Uri uri) {     String mimeType;     if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {         ContentResolver cr = getContentResolver();         mimeType = cr.getType(uri);     } else {         String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri                 .toString());         mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(                 fileExtension.toLowerCase());     }     return mimeType; } 

Note:

On Android KitKat(4.4) don't ask the user to select the sd-card because on this version of Android DocumentProvideris not applicable, hence we have no chance to have access to the sd-card with this approach. Look at the API level for the DocumentProvider https://developer.android.com/reference/android/provider/DocumentsProvider.html
I 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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!