gpt4 book ai didi

android - BitmapFactory.decodeFile 返回 fileNotFoundException

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:13 25 4
gpt4 key购买 nike

我有一个关于 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)

代码 fragment :

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());
}
}

最佳答案

试试这个....图像 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();
}

关于android - BitmapFactory.decodeFile 返回 fileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21907522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com