如何使用uri在RecyclerView中获取图像?

时间:2019-06-06 13:59:30

标签: android

我正在Android Studio中开发一个应用程序。单击“片段”中的按钮以打开对话框,并包含图片和简短描述。 (图片从您的画廊中获取。)单击“确定”关闭对话框,并将图像和说明包含在“片段”中存在的RecyclerView中。但是我编写的这段代码有描述,但是没有图像。我想听听对此的解决方案,所以我问了你一个问题。

感谢您的答复!

为简要说明代码的工作原理,当我从对话框中收到图像时,我将文件的路径(/storage/emulated/0/Pictures/a.jpg)作为Toa​​st消息发送,并且尽快对话框关闭后,图像路径和说明将转换为String形式,并将图像路径从Fragment转换为uri,并使用该图像路径更改为Bitmap,以应用于RecyclerView。

下面是我的代码。

Fragment.java

        btn_addlist = view.findViewById(R.id.btn_addlist);
        btn_addlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment_cus dialog = new DialogFragment_cus();
                dialog.show(getActivity().getSupportFragmentManager(), "tag");
                dialog.setDialogResult(new DialogFragment_cus.OnMyDialogResult() {
//                    result : uri, result2 : dlg_edtName, result3 : dlg_edtSubtitle
                    @Override
                    public void finish(String result, String result2, String result3) {
                        imageUri = result;  RecyclerInputName = result2;
                        RecyclerInputSubtitle = result3;
                        Uri uri = Uri.parse("file://" + imageUri);
                        Bitmap bitmap = null;
                        try {
                            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        RecyclerItem_cus item = new RecyclerItem_cus(bitmap, RecyclerInputName, RecyclerInputSubtitle);
                        mArrayList.add(0, item);
                        mAdapter.notifyDataSetChanged();
                        Toast.makeText(getActivity(), "finish.", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

DialogFragment.java

        dlg_AddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_PICK);
                startActivityForResult(intent, 1);
            }
        });
        dlg_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment_cus fragment = new DialogFragment_cus();
                String input1 = dlg_edtName.getText().toString();
                String input2 = dlg_edtSubtitle.getText().toString();
                if(fragment != null) {
                    if(mDialogResult != null ) {
                        mDialogResult.finish(name_Str, input1, input2);
                    }
                }
                dismiss();
            }
        });
        dlg_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        return view;
    }
    public void setDialogResult(OnMyDialogResult dialogResult){
        mDialogResult = dialogResult;
    }

    public interface OnMyDialogResult{
        void finish(String result, String result2, String result3);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            try {
                name_Str = getImageNameToUri(data.getData());
                InputStream in = getActivity().getContentResolver().openInputStream(data.getData());
                Bitmap img = BitmapFactory.decodeStream(in);
                in.close();
                dlg_ImageView.setImageBitmap(img);
                dlg_ImageView.setBackgroundResource(android.R.color.white);
                Toast.makeText(getActivity(), "name_Str : "+name_Str , Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public String getImageNameToUri(Uri uri) {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
        getActivity().startManagingCursor(cursor);
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(columnIndex);
    }

RecyclerView_cusAdapter.java

@Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, int i) {
        final RecyclerItem_cus item = items.get(i);
        if (item != null) {
            Glide.with(context)
                    .load(item.getImage())
                    .asBitmap()
                    .format(DecodeFormat.PREFER_ARGB_8888)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            myViewHolder.cus_img.setImageBitmap(resource);
                        }
                    });
            myViewHolder.cus_title.setText(item.getTitle());
            myViewHolder.cus_subtitle.setText(item.getSubTitle());
        }
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView cus_title;
        TextView cus_subtitle;
        ImageView cus_img;

        public MyViewHolder(View itemView) {
            super(itemView);

            cus_title = itemView.findViewById(R.id.cus_title);
            cus_subtitle = itemView.findViewById(R.id.cus_subtitle);
            cus_img = itemView.findViewById(R.id.cus_img);
        }
    }

RecyclerItem_cus.java

public class RecyclerItem_cus {
    private Bitmap image;
    private String title;
    private String subTitle;

    public RecyclerItem_cus(Bitmap image, String title, String subTitle) {
        this.image = image;
        this.title = title;
        this.subTitle = subTitle;
    }

    public Bitmap getImage() {
        return image;
    }
    public String getTitle() {
        return title;
    }
    public String getSubTitle() {
        return subTitle;
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试直接将Glide与File对象一起使用:

Glide.with(mContext)
  .load(new File(item.getImage())) // Uri of the picture
  .into(myViewHolder.cus_img);