Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the file name from URI returned from Intent.ACTION_GET_CONTENT?

Tags:

android

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?

like image 639
Viktor Brešan Avatar asked Apr 06 '11 15:04

Viktor Brešan


People also ask

How to GET file name from CONTENT uri?

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.

How to GET file name from url in java?

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.

How do you get real path from URI?

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.


2 Answers

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; } 
like image 93
Stefan Haustein Avatar answered Sep 27 '22 22:09

Stefan Haustein


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();     } } 
like image 43
Ken Fehling Avatar answered Sep 27 '22 21:09

Ken Fehling