如何打开图库并从适配器类中选择图像?

时间:2018-07-06 12:09:26

标签: android

我有一个回收站视图适配器类,在绑定视图持有人中,我想从图库中获取图像并将其设置为图像视图。 在bindview中,有一些问题可以解决,基本上我想打开图库并选择图像并将其设置在imageview上。

 @Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {

    Cheque_Payment chqadd_list = cheques_list.get(position);
    ImageView imageView=holder.imageView;
    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("images/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            contxt.startActivity(intent);
            startActivityForResult(intent,IMG_REQUEST);
        }
    });


    holder.Chq_Amount.setText(cheques_list.get(position).getChequeAmount());
    holder.Chq_No.setText(cheques_list.get(position).getChequeNumber());
    holder.Chq_Date.setText(cheques_list.get(position).getChequeDate());
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == IMG_REQUEST && resultCode == RESULT_OK && data != null) {
        Uri path = data.getData();
        try {

          Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), path);
            imageview.setImageBitmap(bitmap);
            imageview.setVisibility(View.VISIBLE);

            // chx.setChequeImage(imageview.);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试此代码。 使接口进入适配器类以进行点击处理。

    OnItemClick onItemClick;

public void setOnItemClick(OnItemClick onItemClick) {
    this.onItemClick = onItemClick;
}

public interface OnItemClick {
    void getPosition(String data); //pass any things
}

在该单击事件之后进入onBind。

@Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
    // below code handle click event on recycler view item.
    final String data = mStringList.get(position);
    holder.textView.setText(data);
    holder.pulshButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClick.getPosition(data);
        }
    });

}

适配器绑定到回收器视图后,这意味着适配器不为null,然后调用以下方法。

        adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
        @Override
        public void getPosition(String data) {
            // define your camera or gallery image pick method code.
        }
    });

答案 1 :(得分:0)

这里是如何从图库中获取图像并将其设置为图像视图的示例

用于点击处理的适配器类

@Override
public void onBindViewHolder(@NonNull Viewholder holder, final int position) {
 
    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            requestcode = position;
            Intent pickPhoto = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            activity.startActivityForResult(pickPhoto, 1);
        }
    });
    
}

添加更新recyclerview的函数

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode== requestcode)
        {
            if (resultCode == RESULT_OK) {
                Uri selectedFileURI = data.getData();
                Log.e("FileUpload : " , String.valueOf(selectedFileURI));
                imagevalues.get(requestCode).setName(String.valueOf(selectedFileURI));
                AdapterFunction(imagevalues);

            }

    }

}