What I'm trying to do seems very simple, but after a few days of searching I can't quite figure it out.
I have an application that allows the user to select multiple(up to 5) images. I'm using an ImageView
. When the user clicks on the ImageView
, I'd like to allow them the option to
- Select the image from the gallery, or
- Use the camera to capture an image.
I started by using the ACTION_GET_CONTENT
intent, and that works well for getting to the gallery. So then I tried using the ACTION_PICK_ACTIVITY
intent to allow the user to choose camera or gallery:
Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");
Intent camIntent = new Intent("android.media.action.IMAGE_CAPTURE");
pickIntent.putExtra(Intent.EXTRA_INTENT, camIntent);
pickIntent.putExtra(Intent.EXTRA_INTENT, gallIntent)
pickIntent.putExtra(Intent.EXTRA_TITLE, "Select Source");
startActivityForResult(pickIntent, IMAGE_SELECTOR);
But it appears I can only add one EXTRA_INTENT
. The menu show up as expected, but the only options are Gallery and Files....no Camera).
Is there a better/easier way to do this that I'm missing? Thanks for any help.
Building upon David's answer, my two pennies on the
onActivityResult()
part. It takes care of the changes introduced in 5.1.1 and detects whether the user has picked a single or multiple image from the library.For those getting error on 4.4 upward while trying to use the Image selection can use the code below.
Rather than creating a Dialog with a list of Intent options, it is much better to use Intent.createChooser in order to get access to the graphical icons and short names of the various 'Camera', 'Gallery' and even Third Party filesystem browser apps such as 'Astro', etc.
This describes how to use the standard chooser-intent and add additional intents to that.
This is simple by using AlertDialog and Intent.ACTION_PICK
Resolved too big image issue and avoid to out of memory.
Adding my solution - it will return a callback whether it is from the camera or from the galley alongside the intent:
As per David Manpearl answer
https://stackoverflow.com/a/12347567/7226732
we just need to modify onActivityResult() like
and set the capture or pick images in image view.