简体   繁体   中英

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

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.

The problem is based on the API versions you targeting, try checking the build version before calling the intent so from:

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

change it to

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    try {
 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);
 }catch(Exception e){
                        Intent photoPickerIntent = new Intent(this, XYZ.class);
                        startActivityForResult(photoPickerIntent, SELECT_IMAGE_REQUEST);
                    }
                }
    else{
      Intent photoPickerIntent = new Intent(this, XYZ.class);
                        startActivityForResult(photoPickerIntent, SELECT_IMAGE_REQUEST);
    }

Hope that helps.

if (requestCode == UPLOAD_GALLERY_REQ_CODE && resultCode == RESULT_OK && null != data) {
        if(data.getClipData()!=null){
         int count = data.getClipData().getItemCount();
         for(int i=0; i<count; i++){

             //here you can get your multiple images uri's
             // for example, i want array of selected images uri's so : 

             array.add(data.getClipData().getItemAt(i).getUri());

         }
        }else {
         // and here you get the URI of single image (single selected)
            galleryUri.add(data.getData());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM