【问题标题】:BitmapFactory.decodeFile returns fileNotFoundExceptionBitmapFactory.decodeFile 返回 fileNotFoundException
【发布时间】: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


【解决方案1】:

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

【讨论】:

【解决方案2】:

感谢您的回复,它让我朝着正确的方向前进。 最终这篇文章进一步帮助了我:https://stackoverflow.com/a/2636538/3287175

【讨论】:

    【解决方案3】:

    path,您用于在以下行中创建 File onbject...

    File file = new File(path);
    

    在下面一行中,解码位图...

    this.bm = BitmapFactory.decodeFile(path,options);
    

    这甚至不是FolderFile 目录。因此,首先,正确检查您的 path 是否包含正确的 File 路径...这将解决您的问题。

    假设,您将图像保存到名为 ABC.jpg 的 SDCArd 中,那么正确的图像文件路径应如下所示...

    /mnt/sdcard/ABC.png
    

    【讨论】:

    • 'File file = new File(path);'我忘了删除,我不使用它。路径从 onActivityResult 传递给 setPhoto。
    • 我还是不明白,当我使用 MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);那么如何才能找到正确的路径呢?
    猜你喜欢
    • 1970-01-01
    • 2016-10-17
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多