Android:BitmapFactory 返回 java.io.FileNotFoundException 错误,即使图像已保存到外部存储

Android: BitmapFactory returning java.io.FileNotFoundException error, even though image is saved to external storage

我正在尝试制作一个应用程序,用户可以在其中拍照以附加到 post。对于图像捕获、保存和处理部分,我遵循了官方开发人员指南(使用他们的代码片段)。我可以拍摄照片并将其保存到外部存储(我已经在我的硬盘上找到了照片),但是当我需要访问文件以在视图中显示图像(或启动媒体扫描仪意图)时,我收到一条错误消息,指出找不到该文件。

以下是我的部分代码:

文件创建:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

图像捕获意图:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Toast.makeText(getActivity(), ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

显示小图:

private void setPic() {
    // Get the dimensions of the View
    int targetW = 30;
    int targetH = 50;

    // 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;

    // 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);
    img_added.setImageBitmap(bitmap);
}

正在将图像添加到画廊:

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    getActivity().sendBroadcast(mediaScanIntent);
}

然后当我 运行 应用程序时,拍完照片后我在 logcat 中得到了这个:

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

我在清单中设置了写入外部存储的权限,我可以看到保存到我的存储中的图片,所以该文件存在。我真的只是按照官方指南做所有事情,所以我不知道出了什么问题。有人能帮我吗?提前致谢!

编辑:

我改线了

mCurrentPhotoPath = "file:" + image.getAbsolutePath();

mCurrentPhotoPath = image.getAbsolutePath();

所以 createImageFile() 函数现在看起来像这样:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

这解决了我显示图像的问题,但照片仍然没有出现在我的图库中。当我记录传递给媒体扫描意图的 URI 的值时,我得到这个:

file:///storage/emulated/0/Pictures/JPEG_20150731_135706_153874160.jpg

这可能是问题所在(虽然我是使用 Uri.fromFile 方法从图像文件中获取的,所以这应该是一个正确的 URI)?或者还有什么可能导致问题?

更改此行

mCurrentPhotoPath = "file:" + image.getAbsolutePath();

mCurrentPhotoPath = image.getAbsolutePath();