Search code examples
javaandroidandroid-gallery

How to limit multiple image selection from the gallery?


I have implemented adding multiple image selection from the gallery in my project. However, I want to limit the user so he/she can select only 3 images from the gallery.

I have implemented selecting multiple images from the gallery like this:

 `Intent intent = new Intent();
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);`

How can I achieve this?

Thanks.


Solution

  • You can get the count of ClipData when selecting multiple images from and gallery and if that count is greater than 3 you can notify the user about it.

    You can do something like this after selecting images from gallery:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(resultCode == RESULT_OK || resultCode != RESULT_CANCELED){
                  ClipData clipData = data.getClipData();
                  if(clipData.getItemCount() > 3){
                       //notify user here...
                  }
            }
    }