提问者:小点点

BitmapFactory打开失败:ENOENT(没有这样的文件或目录)


我正在尝试使用Android相机意图拍摄一张照片,如本教程所示:http://developer . Android . com/training/Camera/Photo basics . html # taskscaliphoto

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

06-25 14:46:02.228 9070-9070/de.ema.flo.grapp E/BitmapFactory﹕ 无法解码流:java.io.FileNotFoundException:file:/storage/simumed/0/Android/data/de.ema.flo.grapp/files/Pictures/IMG_2015065_14459002。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();
}

这段代码有什么问题吗?我能试着改变什么?


共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;
    }

让我知道这对你有没有帮助。