Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Image Picker Select multiple images from gallery with a maximum limit of 5

I have an app where the user needs to be able to choose multiple pictures to send them somewhere. However, they can only send five images at a time. I need to be able to limit the number of images that they can pick from the gallery through the Image Picker.

To put it in a single sentence: I want to limit the number of images/photos that the user can select in the default image selector from the gallery.

Here is the code that I am using for my image picker:

Intent chooseIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
chooseIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(chooseIntent, 2);

It already keeps track of how many images are selected at the top by default:

Is there a way to set a maximum limit? Like to have a user only be able to select up to 5 images.

like image 734
Alex K Avatar asked May 13 '15 23:05

Alex K


People also ask

How do I select multiple photos from gallery on Android?

The EXTRA_ALLOW_MULTIPLE option is set on the intent through the Intent. putExtra() method: Intent intent = new Intent(); intent. setType("image/*"); intent.

How do I select many images?

Steps to select all Google photos on an Android Device: Go to Google Photos. Tap hold the picture you want to select. Scroll down to the end to select other photos without lifting your finger. The number of selected photos will show up at the top-left of your screen.


1 Answers

It already keeps track of how many images are selected at the top by default:

On that particular device, perhaps. Please understand that there are thousands of Android device models, and manufacturers set up their own UI to replace that of stock apps. Do not assume that all devices will show a count in the action bar.

Is there a way to set a maximum limit? Like to have a user only be able to select up to 5 images.

Not via ACTION_PICK. ACTION_PICK is not documented to support EXTRA_ALLOW_MULTIPLE at all, and so there may be devices that do not allow for multiple selection for that Intent. Even on the Intent actions for which EXTRA_ALLOW_MULTIPLE is part of the protocol (e.g., ACTION_GET_CONTENT), there are no extras for controlling the maximum count.

You are welcome to create your own image-selection UI, based on the results of querying MediaStore, and establish your own limits. There are also several image picker libraries, and one of those may already offer this feature.

like image 116
CommonsWare Avatar answered Oct 13 '22 09:10

CommonsWare