如何将一个图像选择案例与多个图像选择可用条件区分开来?

o2gm4chl  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(221)

我可以从图库中选择多个图像(最多4个)并在回收器视图中显示它们。
当我选择多个图像时,它工作得很好,但当我只选择一个图像时,它会给我 data.getClipdata()==null 案例结果。
这是我的活动代码:

btnPlusImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ActivityCompat.checkSelfPermission(PlusEventActivity.this,
                        Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(PlusEventActivity.this,
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1000);
                }

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, GET_GALLERY_IMAGE);
            }
        });

    /.../

    } //On Create End

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GET_GALLERY_IMAGE && resultCode == RESULT_OK && data != null) {
            uriList = new ArrayList<>();
            if (data.getClipData() == null) {
                Toast.makeText(PlusEventActivity.this, "This device can't select multiple images", Toast.LENGTH_LONG).show();
            } else {
                ClipData clipData = data.getClipData();
                if (clipData.getItemCount() > 4) {
                    Toast.makeText(PlusEventActivity.this, "Maximum 4 images can be selected", Toast.LENGTH_LONG).show();
                } else if (clipData.getItemCount() > 1 && clipData.getItemCount() <= 4) {
                    for (int i = 0; i < clipData.getItemCount(); i++) {
                        uriList.add(clipData.getItemAt(i).getUri());
                    }
                } else if (clipData.getItemCount() == 1) {
                    Toast.makeText(PlusEventActivity.this, "One image is selected", Toast.LENGTH_LONG).show();
                    uriList.add(clipData.getItemAt(0).getUri());
                }
            }
            AdapterPlusEventImage adapter = new AdapterPlusEventImage(uriList, PlusEventActivity.this);
            rvEventImage.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
            rvEventImage.setAdapter(adapter);
        }
    }

我的适配器代码:

public class AdapterPlusEventImage extends RecyclerView.Adapter<AdapterPlusEventImage.ItemViewHolder> {
        public ArrayList<Uri> eventImageList;
        public Context context;

        public AdapterPlusEventImage(ArrayList<Uri> eventImageList, Context context) {
            this.eventImageList = eventImageList;
            this.context = context;
        }

        @NonNull
        @Override
        public AdapterPlusEventImage.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            Context context = parent.getContext();
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.item_plus_event_image, parent, false);
            AdapterPlusEventImage.ItemViewHolder viewHolder = new AdapterPlusEventImage.ItemViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull AdapterPlusEventImage.ItemViewHolder holder, int position) {
            holder.itemPlusEventImage.setImageURI(eventImageList.get(position));
        }

        @Override
        public int getItemCount() {
            return eventImageList.size();
        }

        public class ItemViewHolder extends RecyclerView.ViewHolder {
            ImageView itemPlusEventImage;

            public ItemViewHolder(@NonNull View itemView) {
                super(itemView);
                itemPlusEventImage = itemView.findViewById(R.id.itemPlusEventImage);
            }
        }
    }

当选择一个图像时,我想在“回收器”视图中显示该图像。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题