Skip to content

Instantly share code, notes, and snippets.

@rahulsdhar
Forked from Zhiw/getFilePathUtil
Last active March 6, 2020 09:26
Show Gist options
  • Save rahulsdhar/6800be017dc0a96c0f68e370419323cd to your computer and use it in GitHub Desktop.
Save rahulsdhar/6800be017dc0a96c0f68e370419323cd to your computer and use it in GitHub Desktop.
convert content Uri to file path
public class getRealPathUtil {
@ @TargetApi(Build.VERSION_CODES.KITKAT)
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
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;
}
public static String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
cursor.moveToFirst();
int column_index = cursor.getColumnIndex(proj[0]);
String path = cursor.getString(column_index);
return path;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment