Home > database >  Get NullPointerException when trying load image with Glide
Get NullPointerException when trying load image with Glide

Time:09-24

Im trying to load image from RoomDB with Glide by Uri, but i got NPE. What am i do wrong? Is that Uri path looks right or not? [/storage/emulated/0/Pictures/Title (19).jpg]

UPDATED

errCode

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}

in this code im retrieve data into 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();
                }
            }

that's how im taken Uri path

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

adapter class, i think smth wrong in my Glide expression

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

    }

CodePudding user response:

it says in Glide documentation about permissions

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

so could it be the case that you didn't declare this permission in your android Manifest ?

<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>

It's just a suggestion that came to my head.

CodePudding user response:

make sure you have <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> in menifest

change

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

to

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

thats it.

CodePudding user response:

The answer was simple, im trying to load all Array into uri

before

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

after

Uri uri = Uri.parse(photos.get(0));
  • Related