Select multiple images from android gallery

Dion Segijn picture Dion Segijn · Oct 25, 2013 · Viewed 161.4k times · Source

So basically what i am trying to achieve is opening the Gallery in Android and let the user select multiple images. Now this question has been asked frequently but i'm not satisfied with the answers. Mainly because i found something interesting in de docs in my IDE (i come back on this later) and thereby i don't want to use a custom adapter but just the vanilla one.

Now my code for selecting one image is:

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

Now People on SO and other websites wil tell you you have 2 options:

1) Do not use ACTION_GET_CONTENT but ACTION_SEND_MULTIPLE instead.
This one doesn't work. This one is according to the docs for sending files and not retrieving and that's exactly what it does. When using ACTION_SEND_MULTIPLE i got a window opened at my device where i have to select an application to send my data to. That's not what i want, so i wonder how people got this achieved with this solution.. Do i miss something?

2) Implement an custom Gallery. Now this is my last option i will consider because imho it's not what i am searching for because i have to style it myself AND why the heck you just can't select multiple images in the vanilla gallery?

There must be an option for this.. Now the interesting thing what i'v found is this:
I found this in the docs description of ACTION_GET_CONTENT.

If the caller can handle multiple returned items (the user performing multiple selection), then it can specify EXTRA_ALLOW_MULTIPLE to indicate this.

This is pretty interesting. Here they are referring it to the use case where a user can select multiple items?

Later on they say in the docs:

You may use EXTRA_ALLOW_MULTIPLE to allow the user to select multiple items.

So this is pretty obvious right? This is what i need. But my following question is: Where can i put this EXTRA_ALLOW_MULTIPLE? The sad thing is that i can't find this no where in the developers.android guide and also is this not defined as a constant in the INTENT class.

Anybody can help me out with this EXTRA_ALLOW_MULTIPLE?

Answer

Kyle Shank picture Kyle Shank · Nov 7, 2013

The EXTRA_ALLOW_MULTIPLE option is set on the intent through the Intent.putExtra() method:

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

Your code above should look like this:

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

Note: the EXTRA_ALLOW_MULTIPLE option is only available in Android API 18 and higher.