0

Decoding image file received from Media.EXTRA_OUTPUT in action_pick intent produce file not found exception. I see a lot of similar questions regarding to this but still can't figure out the issue. I face this issue in android Gallery app below Lollipop and the same issue in Google photo app above android version >= 5.

Myfragment.java

if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Helpers.getOutputMediaFileUri());

    Intent startImage = Intent.createChooser(chooseImageIntent, "Select From");

    startImage.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            new Intent[]{takePictureIntent});
    ((Activity) mContext).startActivityForResult(startImage, Constants.REQUEST_CHOOSE_FROM);
} else {
    ((Activity) mContext).startActivityForResult(chooseImageIntent, Constants.REQUEST_IMAGE_GALLERY);
}

Helpers.java

public static String LAST_IMAGE_FILE;
public static final String TEMP_IMAGE_FILE = "TEMP_IMG";

public static File getOutputMediaFile() {
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "In.Touch");

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + TEMP_IMAGE_FILE + "_" + timeStamp + ".jpg");
    LAST_IMAGE_FILE = mediaFile.getAbsolutePath();

    return mediaFile;
}

OnActivityResult

File file = new File(Helpers.LAST_IMAGE_FILE);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);

AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Tixeon
  • 930
  • 1
  • 12
  • 27
  • Please show the code where you are saving the value of `LAST_IMAGE_FILE` either in the saved instance state `Bundle` or in something else that will survive your process being terminated. Also, note that `ACTION_PICK` does not use `EXTRA_OUTPUT` -- instead, you use the `Uri` you get back in `onActivityResult()` with `ContentResolver, `open()`, and `BitmapFactory.decodeStream()` (or, better yet, an [image-loading library](http://android-arsenal.com/tag/46) like Picasso). – CommonsWare Mar 14 '16 at 16:04
  • I had update my code. **LAST_IMAGE_FILE** is static var in **Helpers.java** so as **TEMP_IMAGE_FILE** – Tixeon Mar 15 '16 at 01:42

0 Answers0