Search code examples
androidandroid-intentandroid-contentproviderandroid-image

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?


Solution

  • 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);