Created
November 13, 2015 15:27
-
-
Save Zhiw/bdb4e4f8b8bac4b9d288 to your computer and use it in GitHub Desktop.
convert content Uri to file path
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 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(); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in Kotlin