我尝试使用此方法从URI获取路径:
public String getPath(Uri uri) {
String result;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = uri.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
当我尝试压缩位图时:
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
我收到此错误:
05-18 16:58:56.346 19080-19080 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/IMG_20160518_165856.jpg: open failed: ENOENT (No such file or directory)
05-18 16:58:56.346 19080-19080/E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/IMG_20160518_165856.jpg: open failed : ENOENT (No such file or directory)
05-18 16:58:56.356 19080-19080/E/JHEAD: can't open '/storage/emulated/0/Pictures/IMG_20160518_165856.jpg'
答案 0 :(得分:1)
//Change this
public String getPath(Uri uri) {
String result;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = uri.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
//To this and try
public String getPath(Uri uri) {
String result;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = uri.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
Log.e("Tag", "idx " + idx);
result = cursor.getString(idx);
cursor.close();
}
return result;
}