9

In my app I let users pick a photo from their gallery. I use an intent like this:

Intent pickPictureIntent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

And before I start this intent I check if there is any app that can handle it:

if (pickPictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
    startActivityForResult(pickPictureIntent, SELECT_PICTURE_FROM_GALLERY_REQUEST_CODE);
}

But two of my users get this exception when they try to pick a photo from their gallery:

Exception android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.PICK dat=content://media/external/images/media }

As far I know this happens when there is no activity to handle the intent but as you see I check the possibility of having no activity to handle the intent in my code.

Mostafa
  • 785
  • 2
  • 9
  • 21

1 Answers1

18

Try this:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

This brings up the Documents app. To allow the user to also use any gallery apps they might have installed:

Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");


Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");

Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});

startActivityForResult(chooserIntent, PICK_IMAGE);
Jay Patel
  • 1,051
  • 8
  • 24
  • Are you sure that the ActivityNotFoundException doesn't occur at all with this code? – Mostafa Aug 16 '17 at 07:52
  • There should not be ActivityNotFoundException with this code – Jay Patel Aug 16 '17 at 11:46
  • To complement the answer, AS is currently warning that using pickIntent.setType overwrites what was done in the previous line, so the improvement for this answer is that it uses: pickIntent.setDataAndType(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); – Esteban May 02 '20 at 18:09
  • In case you're wondering where PICK_IMAGE comes from... https://stackoverflow.com/a/5309217/1174169 – cod3monk3y May 05 '22 at 05:01