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

时间:2015-07-28 09:08:32

标签: java android io

我正在关注这个tutorial拍照,保存,缩放并在android中使用它。但是,在尝试打开/检索已保存的图像时,我收到Android: open failed: ENOENT (No such file or directory)错误。经过一些研究后,我发现这篇文章假定这个问题附带的文件中包含其名称中的数字,就像我的名字带有当前时间戳的名字。我检查了图像是否保存在文件目录中并记录以确保用于检索它们的文件名与原始名称匹配。 以下是我的代码中提供错误的部分:

private void setPic(ImageView myImageView) {
    // Get the dimensions of the View
    int targetW = myImageView.getWidth();
    int targetH = myImageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;
    Log.v("IMG Size", "IMG Size= "+String.valueOf(photoW)+" X "+String.valueOf(photoH));

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    myImageView.setImageBitmap(bitmap);
}

以下是日志记录显示的内容:

E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /file:/storage/sdcard0/Pictures/JPEG_20150728_105000_1351557687.jpg: open failed: ENOENT (No such file or directory)

我正在尝试打开名为JPEG_20150728_105000_1351557687.jpg

的图像

1 个答案:

答案 0 :(得分:1)

我下载了您的代码并尝试在我的应用程序中使用相同的代码。找到导致/file:的前缀FileNotFoundException

将以下方法替换您的方法。

    private void setPic(ImageView myImageView) {
    // Get the dimensions of the View
    int targetW = myImageView.getWidth();
    int targetH = myImageView.getHeight();

    String path = mCurrentPhotoPath.replace("/file:","");

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;
    Log.v("IMG Size", "IMG Size= " + String.valueOf(photoW) + " X " + String.valueOf(photoH));

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);
    myImageView.setImageBitmap(bitmap);
}