android 当 select 一张图片不是多张图片时出错

android error when select one image not multiple

我正在尝试从我的图库中获取图像并将它们上传到服务器。我想允许用户 select 来自画廊的多张图片。当我 select 两张或更多图片时,效果很好。但是当我 select 只有一张图片时,它会忽略它并且 return 什么也不会。这是我的代码,当没有剪辑数据为空时我正在打印消息

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


        }

}

在这里我叫打开画廊:

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

问题是基于您定位的 API 版本,请尝试在调用 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 Picture"), 10);

改为

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

希望对您有所帮助。

如果只选择了一张图片,它将不会像选择更多图片那样出现在 ClipData 中。

而不是 data.getData() 将是所选的 Uri

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