4

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?

Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74

3 Answers3

5

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.

Community
  • 1
  • 1
ELITE
  • 5,815
  • 3
  • 19
  • 29
5

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.

Kesha
  • 497
  • 6
  • 19
1

I found a good image and videos selection library called Matisse. You call specify the total number of images or videos that you want a user to select. Selection happens without leaving your app since an activity called MatisseActivity presents all images from the user phone where the user will select the ones he wants until the specified maximum number is reached.

https://github.com/zhihu/Matisse

Matisse.from(MainActivity.this)
        .choose(MimeType.allOf())
        .countable(true)
        .maxSelectable(10)
        .thumbnailScale(0.85f)
        .imageEngine(new GlideEngine())
        .forResult(REQUEST_CODE_CHOOSE);
Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74