Android: 打开失败: ENOENT (No such file or directory) 错误

Android: open failed: ENOENT (No such file or directory) Error

我正在关注这个 tutorial 来拍照、保存、缩放并在 android 中使用它。但是,在尝试 open/retrieve 保存的图像时出现 Android: open failed: ENOENT (No such file or directory) 错误。经过一些研究,我发现这个 post 假设这个问题伴随着名称中包含数字的文件,就像我的文件一样,它的名称带有当前时间戳。我检查了图像是否保存在文件目录中,并登录以确保用于检索它们的文件名与原始名称相匹配。 这是我的代码中出现错误的部分:

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

的图片

我下载了您的代码并尝试在我的应用程序中使用它。发现前缀/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);
}