Android 11<= resolveActivity

244 Views Asked by At

I work on improvement functionality for android app. There is a feature for making photo from app. And for andorid 11<=

takePictureIntent.resolveActivity(getPackageManager())

returns null. I know it's because of higher android version. But when I removed if statement everything working so I wonder why this line was there and what wrong could happened if it will be removed.

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
   //if (true) {
        File photoFile = createImageFile();

        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}
1

There are 1 best solutions below

1
On BEST ANSWER

If there is no matching activity, your startActivityForResult() call should crash with an ActivityNotFoundException.

Wrapping the startActivityForResult() call in a try/catch works for all Android versions.