public static String getFilePathFromUri(Uri uri, Context c) {
try {
String filePath = null;
String scheme = uri.getScheme();
if (scheme != null && scheme.equals("content")) {
ContentResolver contentResolver = c.getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null, null,
null);
cursor.moveToFirst();
filePath = cursor.getString(cursor
.getColumnIndexOrThrow(Images.Media.DATA));
}
return filePath;
} catch (Exception e) {
//java.lang.illegalArgumentException: column'_data' does not exist
return null;
}
}
当图片来自 Picasa 文件夹时,我收到此例外。
答案 0 :(得分:1)
// try this way,hope this will help you...
public String getAbsolutePath(Uri uri) {
if(Build.VERSION.SDK_INT >= 19){
String id = uri.getLastPathSegment().split(":")[1];
final String[] imageColumns = {MediaStore.Images.Media.DATA };
final String imageOrderBy = null;
Uri tempUri = getUri();
Cursor imageCursor = getContentResolver().query(tempUri, imageColumns,
MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
if (imageCursor.moveToFirst()) {
return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
}else{
return null;
}
}else{
String[] projection = { MediaColumns.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
}