Created
May 9, 2019 22:30
-
-
Save enisinanaj/137f63bad5d83a4417421ce5e3859c2b to your computer and use it in GitHub Desktop.
Get real path from android content uri
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String getRealFilePath(final Context context, final Uri uri) { | |
if (null == uri) return null; | |
final String scheme = uri.getScheme(); | |
String data = null; | |
if (scheme == null) | |
data = uri.getPath(); | |
else if (ContentResolver.SCHEME_FILE.equals(scheme)) { | |
data = uri.getPath(); | |
} else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { | |
final Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null); | |
if (null != cursor) { | |
if (cursor.moveToFirst()) { | |
final int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); | |
if (index > -1) { | |
data = cursor.getString(index); | |
} | |
} | |
cursor.close(); | |
} | |
} | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment