Skip to content
Advertisement

android error when select one image not multiple

I am trying to get images from my Gallery and upload them to the server. I want to allow the user to select multiple images from gallery. when I select two or more images it works very well. But when I select one image only it ignore it and return nothing. Here is my code and I am printing message when no clip Data is null

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (requestCode == 10 && resultCode == Activity.RESULT_OK) {

            if(data!=null)
            {
                ClipData clipData = data.getClipData();

                if (clipData != null) {
                    bitmaps_group=new Bitmap[clipData.getItemCount()];
                    for (int i = 0; i < clipData.getItemCount(); i++) {

                        ClipData.Item item = clipData.getItemAt(i);
                        Uri uri = item.getUri();

                        try {
                            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
                            bitmaps_group[i]=MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if(i==4)
                            i=clipData.getItemCount()+1;
                    }
                    new Encode_image().execute();
                }
                else
                    Toast.makeText(getActivity(),"error",Toast.LENGTH_SHORT).show();
            }


        }

}

here where i call open the gallery:

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        System.out.println("r1 clikcid");

        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"), 10);
    }
});

Advertisement

Answer

If only one image is selected it will not be in ClipData as is the case with selecting more images.

Instead data.getData() will be the Uri of the selected one.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement