尝试使用 Glide 加载图像时出现 NullPointerException

Get NullPointerException when trying load image with Glide

我正在尝试通过 Uri 使用 Glide 从 RoomDB 加载图像,但我遇到了 NPE。我做错了什么?该 Uri 路径是否正确? [/storage/emulated/0/Pictures/Title (19).jpg]

已更新

错误码

2021-09-24 01:21:07.048 24013-24013/com.example.rempractice W/Glide: Load failed for [content://media/external/images/media/71] with size [316x294]
    class com.bumptech.glide.load.engine.GlideException: Failed to load resource
      Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{StringUri->Object->Drawable}, LOCAL
        Cause (1 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{StringUri->Drawable->Drawable}
        Cause (2 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{StringUri->Bitmap->Drawable}

在此代码中,我将数据检索到 RoomDB

        ARB.contRem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ARB.remText.getText().toString().equals("")){
                    ARB.err.setText("errCode");
                }
                else if(ARB.dateFromCalendar.getText().toString().equals("")){
                    ARB.err.setText("errCode");
                }
                else {
                    Bitmap bitmap = ((BitmapDrawable)ARB.addImageRem.getDrawable()).getBitmap();

                    Uri uri = getImageUri(getContext(), bitmap);
                    list.add(uri.toString());

                    ARVM.addReminder(ARB.remText.getText().toString(),
                            ARB.dateFromCalendar.getText().toString(),
                            false,
                            list);

                    Navigation.findNavController(v).popBackStack();
                }
            }

这就是我采用 Uri 路径的方式

     public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

DTO class

    @Override
    public List<String> getImages() {
        if (super.getImages() == null || super.getImages().isEmpty()) {
            super.setImages(new Gson().fromJson(this.images, List.class));
        }
        return super.getImages();
    }
    @Override
    public void setImages(List<String> images) {
        super.setImages(images);
        this.images = new Gson().toJson(images);
    }

适配器class,我认为我的 Glide 表达式有问题

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


        List<String> photos = rems.get(position).getImages();
        Uri uri = Uri.parse(Arrays.toString(photos.toArray()));

        GlideApp.with(hostFragment)
                .load(uri)
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.NONE))
                .into(holder.binding.imageView);


        holder.binding.textrow.setText(rems.get(position).getTextRem());
        holder.binding.daterow.setText(rems.get(position).getDateRem());
        holder.binding.isdone.setChecked(rems.get(position).isDone());

    }

它在 Glide documentation 中提到了权限

To load images from local folders like DCIM or Pictures, you’ll need to add the READ_EXTERNAL_STORAGE permission

所以会不会是您没有在 android 清单中声明此权限?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.package.name"

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application>
      ...
    </application>
</manifest>

这只是我想到的一个建议。

确保清单中有 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

改变

GlideApp.with(hostFragment)
                .load(uri)
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.NONE))
                .into(holder.binding.imageView);

Glide.with(context).load(uri).into(holder.imageView);

就是这样。

答案很简单,我正在尝试将所有数组加载到 uri

之前

Uri uri = Uri.parse(Arrays.toString(photos.toArray()));

之后
Uri uri = Uri.parse(photos.get(0));