I am using 3rd party file manager to pick a file (PDF in my case) from the file system.
This is how I launch the activity:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(getString(R.string.app_pdf_mime_type)); intent.addCategory(Intent.CATEGORY_OPENABLE); String chooserName = getString(R.string.Browse); Intent chooser = Intent.createChooser(intent, chooserName); startActivityForResult(chooser, ActivityRequests.BROWSE);
This is what I have in onActivityResult
:
Uri uri = data.getData(); if (uri != null) { if (uri.toString().startsWith("file:")) { fileName = uri.getPath(); } else { // uri.startsWith("content:") Cursor c = getContentResolver().query(uri, null, null, null, null); if (c != null && c.moveToFirst()) { int id = c.getColumnIndex(Images.Media.DATA); if (id != -1) { fileName = c.getString(id); } } } }
Code snippet is borrowed from Open Intents File Manager instructions available here:
http://www.openintents.org/en/node/829
The purpose of if-else
is backwards compatibility. I wonder if this is a best way to get the file name as I have found that other file managers return all kind of things.
For example, Documents ToGo return something like the following:
content://com.dataviz.dxtg.documentprovider/document/file%3A%2F%2F%2Fsdcard%2Fdropbox%2FTransfer%2Fconsent.pdf
on which getContentResolver().query()
returns null
.
To make things more interesting, unnamed file manager (I got this URI from client log) returned something like:
/./sdcard/downloads/.bin
Is there a preferred way of extracting the file name from URI or one should resort to string parsing?
Retrieve a file's name and size. The FileProvider class has a default implementation of the query() method that returns the name and size of the file associated with a content URI in a Cursor . The default implementation returns two columns: DISPLAY_NAME.
URL getFile() method in Java with Examples The getFile() function is a part of URL class. The function getFile() returns the file name of a specified URL. The getFile() function returns the path and the query of the URL.
Uri selectedImageURI = data. getData(); String realPath = getRealPathFromURI(selectedImageURI); 1) In fragment you can use function like this way.. 2) And activity you can use function like that.
developer.android.com has nice example code for this: https://developer.android.com/guide/topics/providers/document-provider.html
A condensed version to just extract the file name (assuming "this" is an Activity):
public String getFileName(Uri uri) { String result = null; if (uri.getScheme().equals("content")) { Cursor cursor = getContentResolver().query(uri, null, null, null, null); try { if (cursor != null && cursor.moveToFirst()) { result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); } } finally { cursor.close(); } } if (result == null) { result = uri.getPath(); int cut = result.lastIndexOf('/'); if (cut != -1) { result = result.substring(cut + 1); } } return result; }
I'm using something like this:
String scheme = uri.getScheme(); if (scheme.equals("file")) { fileName = uri.getLastPathSegment(); } else if (scheme.equals("content")) { String[] proj = { MediaStore.Images.Media.TITLE }; Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null); if (cursor != null && cursor.getCount() != 0) { int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE); cursor.moveToFirst(); fileName = cursor.getString(columnIndex); } if (cursor != null) { cursor.close(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With