简体   繁体   English

选择一个图像而不是多个图像时出现android错误

[英]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.如果只选择了一张图像,它不会像选择更多图像那样在ClipData中。

Instead data.getData() will be the Uri of the selected one.相反data.getData()将是所选的Uri

The problem is based on the API versions you targeting, try checking the build version before calling the intent so from:问题基于您定位的 API 版本,请尝试在调用意图之前检查构建版本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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