1

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);
        }
    }
}
Tomasz Stępnik
  • 87
  • 1
  • 11
  • 1
    I'm having a similar issue with resolveActivity prior to trying to launch an URL in Android 11. There's some info here: https://developer.android.com/training/package-visibility – HilaryN May 28 '22 at 21:49

1 Answers1

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have a similar question posted for resolveActivity() here: https://stackoverflow.com/questions/74282516/android-add-resolveactivity-as-well-as-default-apps-setpackage-code. Any thoughts or ideas for the question? – AJW Mar 10 '23 at 04:01