Saturday, 16 May 2015

Get image path from URI, Android KitKat new storage access framework

 private String getPath(Uri uri)
   {
if (uri== null) {
return null;
}

String[] projection = { MediaStore.Images.Media.DATA };

Cursor cursor;
if (Build.VERSION.SDK_INT >= 19) {

String wholeID = DocumentsContract.getDocumentId(uri);

String id = wholeID.split(":")[1];

String sel = MediaStore.Images.Media._ID + "=?";

cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI                                         , projection,sel , new String[] { id}, null);
} else 
{
cursor = getContentResolver().query(uri, null, null, null,null);

}
String path = null;
try {
         int column_index=
                        cursor.getColumnIndex(MediaStore.Images.Media.DATA);

cursor.moveToFirst();
path = cursor.getString(column_index).toString();
cursor.close();
} catch (NullPointerException e) {

}
return path;
}