在 Android 中打开图片时发现 FileNotFound

FileNotFound when opening picture in Android

我正在构建一个需要打开相机拍摄的照片的应用程序。我按照官方 android guide 并按照他们说的做了,但我不断收到此错误消息:

Unable to decode stream: java.io.FileNotFoundException: file:/storage/emulated/0/Pictures/JPEG_test_-1259913402.jpg: open failed: ENOENT (No such file or directory)

我的代码如下所示:

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(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
            System.err.println("An error occurred: " + ex);
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        //try {
            Bitmap imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
            System.out.println(imageBitmap);
            testViewer.setImageBitmap(imageBitmap);
            //bc = new BarDecoder(imageBitmap);
        //} catch (NotFoundException | FormatException e){
          //  System.err.println("An error occurred: " + e);
        //}
    } else {
        System.out.println("Cancelled!");
    }
}

public void scanImage(View view){
    dispatchTakePictureIntent();
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = "test";
    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();
    System.out.println("Created!!");
    return image;
}

我可以看到这些图片确实是使用文件资源管理器创建的,但我的应用程序找不到它们。我还在我的清单文件中包含了这些权限:

<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

有什么问题吗?

通过从 mCurrentPhotoPath 中删除 "file:" 解决了这个问题。 Android的官方指南说这是必要的,但显然这是行不通的!