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
?
Try this one IntentChooser. Just add some lines of code, I did the rest for you.
PS: as mentioned at the answers above, EXTRA_ALLOW_MULTIPLE is only available for API >= 18. And some gallery apps don't make this feature available (Google Photos and Documents (
com.android.documentsui
) work.I also had the same issue. I also wanted so users could take photos easily while picking photos from the gallery. Couldn't find a native way of doing this therefore I decided to make an opensource project. It is much like MultipleImagePick but just better way of implementing it.
https://github.com/giljulio/android-multiple-image-picker
I hope this answer isn't late. Because the gallery widget doesn't support multiple selection by default, but you can custom the gridview which accepted your multiselect intent. The other option is to extend the gallery view and add in your own code to allow multiple selection.
This is the simple library can do it: https://github.com/luminousman/MultipleImagePick
Update:
From @ilsy's comment, CustomGalleryActivity in this library use
manageQuery
, which is deprecated, so it should be changed togetContentResolver().query()
andcursor.close()
like this answerInitialize instance:
In onActivityResult You have to write this, If-else 2 block. One for single image and another for multiple image.
Most important part, Get Image Path from uri:
Hope this can help you.
Hi below code is working fine.
You want to more clarifications. http://mylearnandroid.blogspot.in/2014/02/multiple-choose-custom-gallery.html
A lot of these answers have similarities but are all missing the most important part which is in
onActivityResult
, check ifdata.getClipData
is null before checkingdata.getData
The code to call the file chooser:
The code to get all of the images selected:
Note that Android's chooser has Photos and Gallery available on some devices. Photos allows multiple images to be selected. Gallery allows just one at a time.