android中轉換content://media/external/images/media/539163為/storage/emulated/0/DCIM/Camera/IMG_20160807_

--LIANG--發表於2016-09-20

在上傳照片時遇到上傳路徑異常:content://media/external/images/media/539163
轉成:/storage/emulated/0/DCIM/Camera/IMG_20160807_133403.jpg路徑;


PHOTO_DATA(content://media/external/images/media/539163)

Log.d(“log”,”照片路徑資訊:”+String.valueOf(getRealPathFromUri(this,PHOTO_DATA)));


解決方法:
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);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}

相關文章