Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the maximum number of items to be returned by an intent

I have an intent that can be used to allow the user to select some images in an image app like galley(or any other present in the user's device).

I want the user to select ONLY 10 images but i don't know how i can set this maximum on the intent. i have tried to see if i can use ClipData but clipdata doesn't have methods to set maximum number of items.

ClipboardManager manager = getSystemService(Context.CLIPBOARD_SERVICE)
ClipData clipdata = manager.getPrimaryClip();// in short whether i get 
or i create a clipdata, there are no methods to set maximum number of
items to be held into that clip

here is my intent.

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, SELECT_PHOTO); 

How can i limit the user to select only 10 photos?

like image 797
Edijae Crusar Avatar asked Dec 19 '22 20:12

Edijae Crusar


2 Answers

using ClipData check returned item count

ClipData mClipData = data.getClipData();
// here you can check how many images user has selected.
if(mClipData.getItemCount() >= 10) {
    // do needful here
    Log.e("APP_TAG", "Greater than THRESHOLD.");
    // show some error
    return;
}

refer this question for more details.

like image 185
ELITE Avatar answered Dec 21 '22 11:12

ELITE


You can try this library : https://github.com/sangcomz/FishBun?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=2785

It allows you to set maximum number of images that can be shared. You can also customize PickerActivity by setting color of actionBar & statusBar. Also can set Your custom message when reached to specified limit.

Hope this will help you.

like image 23
Kesha Avatar answered Dec 21 '22 11:12

Kesha