I have Uri for Image file.
I use this code for gets file path from Uri:
public String getRealPathFromURI(Uri contentUri) {
Cursor cursor = null;
A simpler version of the accepted answer that will detect the API level and use the correct method :
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getFilePath(Context context, Uri uri)
{
int currentApiVersion;
try
{
currentApiVersion = android.os.Build.VERSION.SDK_INT;
}
catch(NumberFormatException e)
{
//API 3 will crash if SDK_INT is called
currentApiVersion = 3;
}
if (currentApiVersion >= Build.VERSION_CODES.KITKAT)
{
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst())
{
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB)
{
String[] proj = {MediaStore.Images.Media.DATA};
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
uri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if (cursor != null)
{
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
else
{
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
int column_index
= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
public static int getIdFromUri(Uri uri, String constantId) { Cursor c = ContentMaster.getContentResolver().query(uri, new String[]{constantId}, null, null, null);
int res = -1;
if(c != null && c.getCount() > 0)
{
c.moveToFirst();
res = c.getInt(0);
c.close();
}
return res;
}
public static Uri getUriWithId(Uri baseUri,int contactId)
{
Uri u = ContentUris.withAppendedId(baseUri, contactId);
if(u != null)
return u;
return Uri.EMPTY;
}
public static String getRealPathFromURI(Uri uri)
{
if(uri == null)
return "";
if ("file".equalsIgnoreCase(uri.getScheme()))
return uri.getPath();
String[] pro = { MediaStore.Images.Media.DATA };
String result = null;
Cursor cursor;
if(VERSION.SDK_INT > 18)
{
//String wholeID = DocumentsContract.getDocumentId(uri);
//String id = wholeID.split(":")[1];
String id = String.valueOf(getIdFromUri(uri, Images.Media._ID));
String[] column = { MediaStore.Images.Media.DATA };
String where = MediaStore.Images.Media._ID + "=?";
Uri u = uri;
if(isMediaDocument(uri))
{
if (getUriMediaDocumentType(uri).equals("image"))
{
if(isExternalStorageDocument(uri))
u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
else
u = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
}
else if (getUriMediaDocumentType(uri).equals("video"))
{
if(isExternalStorageDocument(uri))
u = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
else
u = MediaStore.Video.Media.INTERNAL_CONTENT_URI;
}
else if (getUriMediaDocumentType(uri).equals("audio"))
{
if(isExternalStorageDocument(uri))
u = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
else
u = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
}
}
else if(isDownloadsDocument(uri))
{
u = getUriWithId(Uri.parse("content://downloads/public_downloads"), Integer.getInteger(id));
}
cursor = getContentResolver().query(u, column, where, new String[]{id}, null);
}
else if(VERSION.SDK_INT > 10)
{
CursorLoader cursorLoader = new CursorLoader(SystemMaster.getContext(), uri, pro, null, null, null);
cursor = cursorLoader.loadInBackground();
}
else
{
cursor = SystemMaster.getContext().getContentResolver().query(uri, pro, null, null, null);
}
if(cursor != null && cursor.getCount() > 0)
{
cursor.moveToFirst();
result = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
}
return result;
}
public static boolean isExternalStorageDocument(Uri uri)
{
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri)
{
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri)
{
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static String getUriMediaDocumentType(Uri uri)
{
if(isMediaDocument(uri))
{
//TODO
return "image";
}
return "";
}