简体   繁体   中英

How to properly error-catch Android camera activity when using the ACTION_IMAGE_CAPTURE Intent

In Android, taking a picture can be done with this basic setup as seen here .

protected void dispatchTakePictureIntent(){
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

        ...
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        //do stuff with the output file
    }
}

However, third-party camera apps are unpredictable and cannot be guaranteed to play nice .

What is the correct way to validate the output / catch any errors? Should I be wrapping startActivityForResult inside of a try/catch block? What are the various types of exceptions I should be trying to catch and what should I do with them?

Should I be wrapping startActivityForResult inside of a try/catch block?

Yes, though this has nothing to do with ACTION_IMAGE_CAPTURE specifically. The user may not have access to a camera app (eg, the user is running under a restricted profile on a shared device). Any time you call startActivity() or startActivityForResult() with an implicit Intent , you need to deal with the possibility that there is no available app.

What are the various types of exceptions I should be trying to catch and what should I do with them?

In the answer that you linked to, I was pointing out crashes in the camera app . You cannot catch those, as that is not your app.

Beyond ActivityNotFoundException , you also need to think about Android 6.0 runtime permissions, as you need the CAMERA permission to start an ACTION_IMAGE_CAPTURE activity. Usually, though, this will not involve an exception handler.

The other validations would be not based on exceptions, but based on flawed results:

  • If you supplied EXTRA_OUTPUT , is there any content at the Uri you specified? In theory, if the user did not cancel out of the camera app, there will be, but that is not guaranteed, due to camera app bugs.

  • If there is is content, is it plausibly an image file? For example, you might use BitmapFactory with inJustDecodeBounds to see if the image really is an image.

  • If there is no content, did you happen to get a Uri back in the result Intent in onActivityResult() ? The buggy camera app might have stored the image where it wanted to and supplied the Uri to it, even though this is way out of spec for ACTION_IMAGE_CAPTURE .

  • If there is no content, and there is no Uri , does getExtras("data") return a non- null value? In that case, you at least got a thumbnail and can save it yourself, even though the image quality may suck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM