Shivam Singh
Shivam Singh

Reputation: 167

How to select multiple images from android gallery separately

What I want to accomplish is to select a sperate image whenever I click on a separate Imageview. For example, If a click on ImageView_1 I can select one image from the gallery and if I click on Imagview_2 I can select a separate image from the gallery. I have seen there are already many answers to this question but they are all different from what is I want to do. In the previous answers, they get all the images as a list in OnActivity Results and all the images are selected at once from the gallery.

My code

Dependency Used

implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.+'

 private void ImageOnclick(){
    
            image_profile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ContextCompat.checkSelfPermission(Upload_New_Product.this,
                            Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                        openFileChooser();
                    } else {
                        requestStoragePermission();
                    }
                }
            });
    
            image_profile2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if (ContextCompat.checkSelfPermission(Upload_New_Product.this,
                            Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                        openFileChooser2();
                    } else {
                        requestStoragePermission();
                    }
    
                }
            });
    
    
    
        }
    
        private void openFileChooser() {
    
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, PICK_IMAGE_REQUEST);
    
    
        }
    
        private void openFileChooser2() {
    
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, PICK_IMAGE_REQUEST2);
    
        }
    
        @RequiresApi(api = Build.VERSION_CODES.P)
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
    
            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
                ImageUri = data.getData();
    
                CropImage.activity(ImageUri)
                        .setGuidelines(CropImageView.Guidelines.ON)
                        //         .setAspectRatio(1, 1)
                        .start(this);
    
            } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
    
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                if (resultCode == RESULT_OK) {
                    resultUri = result.getUri();
                    if (Build.VERSION.SDK_INT >= 29) {
                        try {
                            bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), resultUri));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        // Use older version
                        try {
                            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), resultUri);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                
                    //setImage_profile();
                    resized = Bitmap.createScaledBitmap(bitmap, 600, 600, true);
                    image_profile.setImageBitmap(resized);
                
                } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                    Exception error = result.getError();
                }
    
            }
    
    
            //  UploadingImage();
            //  UploadingThumbnailImage();
    
    
    
            if (requestCode == PICK_IMAGE_REQUEST2 && resultCode == RESULT_OK && data != null) {
                ImageUri2 = data.getData();
    
                CropImage.activity(ImageUri2)
                        .setGuidelines(CropImageView.Guidelines.ON)
                        //         .setAspectRatio(1, 1)
                        .start(this);
    
            } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
    
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
    
                if (resultCode == RESULT_OK) {
                    resultUri2 = result.getUri();
    
    
                    if (Build.VERSION.SDK_INT >= 29) {
                        try {
                            bitmap2 = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), resultUri2));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        // Use older version
                        try {
                            bitmap2 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), resultUri2);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
    
                    resized2 = Bitmap.createScaledBitmap(bitmap2, 600, 600, true);
                    image_profile2.setImageBitmap(resized2);
                    //setImage_profile2();
                    //  UploadingImage();
    
                } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                    Exception error = result.getError();
                }
    
            }
    }

Upvotes: 0

Views: 3473

Answers (1)

Kuvonchbek Yakubov
Kuvonchbek Yakubov

Reputation: 749

You missed this line:

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

And activity result should be like this:

   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PICK_IMAGE_REQUEST) {
            if(resultCode == Activity.RESULT_OK) {
                if(data.getClipData() != null) {
                    int count = data.getClipData().getItemCount();

                    for(int i = 0; i < count; i++)
                    Uri imageUri = data.getClipData().getItemAt(i).getUri();
                    //TODO: do something; here is your selected images
                }
            } else if(data.getData() != null) {
                String imagePath = data.getData().getPath();
                //TODO: do something
            }
        }
    }

Intent:

       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 images"), PICK_IMAGE_REQUEST);

Upvotes: 1

Related Questions