我有一个关于BitMapFactory.decodeFile的问题。
在我的应用中,我希望用户能够从他/她的设备中选择图像或拍照。然后必须在ImageView(medica_photo)中显示。我也尝试缩小图像,因为在我以前的代码中我得到了OutOfMemory错误。
问题是选择图片后,我的应用会抛出此异常:
FileNotFoundException: Unable to decode stream:
java.io.FileNotFoundException: /content:/media/external/images/media/1499:
open failed: ENOENT (No such file or directory)
代码段:
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());
}
}
答案 0 :(得分:3)
试试这个....图片uri
Uri imageUri = data.getData();
try {
bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
iv_profile.setImageBitmap(bm);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 1 :(得分:1)
感谢您的回复,它让我朝着正确的方向前进。 最后这篇文章进一步帮助了我:https://stackoverflow.com/a/2636538/3287175
答案 2 :(得分:0)
path
,用于在以下行中创建File
onbject ...
File file = new File(path);
并在下面的行中解码位图...
this.bm = BitmapFactory.decodeFile(path,options);
甚至不是Folder
或File
目录。因此,首先,请检查您的path
是否包含正确的File
路径......这样可以解决您的问题。
假设您将图像保存到名为ABC.jpg
的SDCArd中,那么正确的图像文件路径应如下所示......
/mnt/sdcard/ABC.png