【问题标题】:BitmapFactory open failed: ENOENT (No such file or directory)BitmapFactory 打开失败:ENOENT(没有这样的文件或目录)
【发布时间】:2015-06-25 13:09:13
【问题描述】:

我正在尝试使用 Android Camera Intent 拍照,如本教程所示:http://developer.android.com/training/camera/photobasics.html#TaskScalePhoto

这张照片拍得很完美,并且在给定的路径上也很安全。但无论如何我得到以下错误:

06-25 14:46:02.228 9070-9070/de.ema.flo.grapp E/BitmapFactory﹕ 无法解码流:java.io.FileNotFoundException: 文件:/storage/emulated/0/Android/data/de.ema.flo.grapp/files/Pictures/IMG_20150625_144559002.JPG: 打开失败:ENOENT(没有这样的文件或目录)

创建图像:

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(new Date());
    File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = new File(storageDir, "IMG_" + timeStamp + ".JPG");
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

在 Intent 中拍照后调用以下代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ACTION_TAKE_PHOTO_B && resultCode == Activity.RESULT_OK) {
        if (mCurrentPhotoPath != null) {
            ImageView mImageView = (ImageView) getActivity().findViewById(R.id.grillplatz_erstellen_image);

            Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);

            // Associate the Bitmap to the ImageView
            mImageView.setImageBitmap(bitmap);
            mCurrentPhotoPath = null;
        }
    }
}

我也尝试过这个。但结果是一样的:FileNotFoundException...

try {
    InputStream is = new FileInputStream(file);
    Bitmap bitmap = BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
    e.printStacktrace();
}

这段代码有问题吗?我可以尝试改变什么?

【问题讨论】:

  • 如果从mCurrentPhotoPath 中删除"file:" + 会怎样?
  • 谢谢@Klotor 删除"file:" + 有效!令人难以置信的是,这让我尝试了这么久......
  • sweet :) 如果你发现自己需要创建一个文件 URI,你需要去"file://" 然后是路径("file:///" 如果路径不以斜杠开头)。无论如何,干得好:)
  • @Klotor 删除 file:// 帮助了我。虽然我没有在我的路径中添加file:

标签: java android image filenotfoundexception bitmapfactory


【解决方案1】:

请检查更新后的代码。

    private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(new Date());
        File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if(!storageDir.isexist)
storageDir.mkdirs();
        File image = new File(storageDir, "IMG_" + timeStamp + ".JPG");
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

让我知道它是否对你有帮助。

【讨论】:

  • 这并没有真正帮助我。已成功创建的文件夹和文件。起作用的是克洛托斯的回答。还是谢谢你!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-23
  • 2016-07-05
  • 2013-02-04
相关资源
最近更新 更多