【发布时间】:2014-02-20 12:15:00
【问题描述】:
我有一个关于 BitMapFactory.decodeFile 的问题。
在我的应用中,我希望用户能够从他/她的设备中选择图像或拍照。然后必须将其显示在 ImageView (medication_photo) 中。我也尝试缩小图像,因为在我之前的代码中我遇到了 OutOfMemory 错误。
问题是我的应用选择图片后抛出这个异常:
FileNotFoundException: Unable to decode stream:
java.io.FileNotFoundException: /content:/media/external/images/media/1499:
open failed: ENOENT (No such file or directory)
代码sn-p:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_PHOTO) {
if (resultCode == RESULT_OK) {
String result = data.getDataString();
setPhoto(result);
}
}
// browse photo
if (requestCode == BROWSE_PHOTO) {
if (resultCode == RESULT_OK) {
String result = data.getDataString();
setPhoto(result);
}
if (resultCode == RESULT_CANCELED) {
}
}
}
private void setPhoto(String path) {
// re-scale image first
try {
File file = new File(path);
Log.e("MedicationAdd.setPhoto->file",file.getAbsolutePath()); // this did not work
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path,options); // FileNotFound
options.inSampleSize = scaleDownBitmap(options, options.outWidth, options.outHeight);
Log.e("MedicationAdd.setPhoto->path",path);
options.inJustDecodeBounds = false;
this.bm = BitmapFactory.decodeFile(path,options); // FileNotFound
this.medication_photo.setImageBitmap(this.bm);
} catch (Exception e) {
Log.e("MedicationAdd.setPhoto", e.toString());
}
}
【问题讨论】:
-
您确定要加载的照片确实位于您要打开它的位置吗?
-
在 setPhoto 函数中,这行代码确实有效:this.bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(path));所以我猜它就在那里。
标签: android filenotfoundexception bitmapfactory