Search code examples
androidandroid-cameraandroid-contentprovider

Camera doesn't create file for GetBitmap or BitmapFactory to review


Short Version

For this line of code:

//mImageBitmap = MediaStore.Images.Media.getBitmap(contentResolver, mUri);
//Altered at the advice of CommonsWare
Bitmap imageBitmap = BitmapFactory.decodeFile(mUri);

I receive back the exception: E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException.

Longer version

public class MainActivity extends Activity {
    static final String EX_TAG = "Exception";
    static final String ACT_TAG = "Action";
    static final int REQUEST_IMAGE_CAPTURE = 1;
    private ImageView mImageView;
    private String mUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = (ImageView) findViewById(R.id.ivPreview);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
        }
    }

    protected void onClick(View view) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (cameraIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (Exception ex) {
                Log.i(EX_TAG, Log.getStackTraceString(ex));
            }

            if (photoFile != null) {
                cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Uri photoUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".provider", photoFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                mUri = photoUri.toString(); // content://com.suamere.phototaker.provider/external_files/Pictures/JPEG_20180128_081539_1869847943162210201.jpg
                try {
                    Log.i(ACT_TAG, "startActivityForResult");
                    startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
                } catch (Exception ex) {
                    Log.i(EX_TAG, Log.getStackTraceString(ex));
                    finish();
                }
            }
        }
    }

    private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        return File.createTempFile(imageFileName, ".jpg", storageDir);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            try {
                Log.i(ACT_TAG, mUri); // content://com.suamere.phototaker.provider/external_files/Pictures/JPEG_20180128_081539_1869847943162210201.jpg
                Bitmap imageBitmap = BitmapFactory.decodeFile(mUri);
                mImageView.setImageBitmap(imageBitmap);
            } catch (Exception ex) {
                Log.i(EX_TAG, Log.getStackTraceString(ex));
            }
        }
    }
}

Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>

When I click the button, the camera comes up. I take a photo and return to my activity. onResultActivity, requestCode is 1 and resultCode is -1, as expected. And the uri is an expected path.


Solution

  • Possibly the file exists. Your code does not actually use the file, and you are not passing it via EXTRA_OUTPUT to the camera app, but perhaps that code is just missing from your question.

    Beyond that:

    • mCurrentPhotoPath is a path, not a Uri, and so Uri.parse() will not work well.

    • Use BitmapFactory.decodeFile(), not MediaStore.Images.Media.getBitmap(), to load a bitmap from a file. Or, better yet, use an image-loading library, like Glide or Picasso, so that you can load the image easily on a background thread, rather than freezing your UI as you are attempting to do now.

    FWIW, here is a complete sample app showing using ACTION_IMAGE_CAPTURE with EXTRA_OUTPUT.

    UPDATE

    Bitmap imageBitmap = BitmapFactory.decodeFile(mUri);
    

    mUri is a Uri. It is not a file. Pass in the path to the File. You would get it by holding onto the File returned by createImageFile().