0

I take a photo and save result in a file. The resulting photo exists, I can open it with file manager. However, I cannot convert that file into a valid Bitmap. I've tried different ways, including BitmapFactory, but it always returns a null Bitmap.

Resulting URI, i.e. mImageUri, is

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

and path

/storage/emulated/0/Pictures/IMG_20150126_180346_1663372760.jpg

Code is below.

Utils class to create new image file

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

    return image;
}

Creating mImageUri in onCreate()

try {
        mPhotoFile = Utils.createImageFile();
        mImageUri = Uri.fromFile(mPhotoFile);
    } catch (IOException ex) {
        Toast.makeText(this, getString(R.string.error_something_wrong_happened), Toast.LENGTH_LONG).show();
    }

Invoking Camera Intent

Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePhotoIntent.resolveActivity(getPackageManager()) != null && mImageUri != null) {

         takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
         startActivityForResult(takePhotoIntent, REQUEST_CAMERA);
}

Parsing results

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
    //Unable to get Bitmap from mImageUri
    try {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageUri);
        Log.d(TAG, "bitmap exists " + bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }
    }

}

Android version is 4.4.4

Roman
  • 2,079
  • 4
  • 35
  • 53

1 Answers1

1

Maybe is late but I fixed my issue with "bitmap=BitmapFactory.decodeFile(uri);" The crazy thing is that I didn't need to put "file://" bcos BitmapFactory accept straight way "/storage/emulated/......../bla.jpeg"

Before I was trying to use:
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(uri));

But didn't works....

Alexiscanny
  • 581
  • 7
  • 15