2

I want to load bitmap from server using Glide. If it's success then put it in my ImageView then add it to my ArrayList. If it's fail (broken url, no internet, etc) then set ImageView to my default image. But Glide's listener not working at all, also LogCat not show "onException" and "onResourceReady" at all. What's wrong with my code?

Glide.with(activity.getApplicationContext())
        .load(MainActivity.URL_SERVER + "/asset/" + imageUrl.getString(i))
        .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                ivImage.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.noimage));
                return false;
            }
            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                ivImage.setImageDrawable(resource);
                imageLoadedBitmap.add(((GlideBitmapDrawable) resource).getBitmap());
                return false;
            }
        });
zihadrizkyef
  • 1,849
  • 3
  • 23
  • 46
  • Possible duplicate of [Glide cannot resolve asBitmap()](https://stackoverflow.com/questions/46199440/glide-cannot-resolve-asbitmap) – Arash Afsharpour May 01 '18 at 15:27

3 Answers3

2

As of now, nothing in that method chain is actually triggering the image loading operation. You could use .into(ivImage) at the end and just use listener for adding bitmap to array. Or you could use .preload() for more customisation. Also, use .error(R.drawable.noimage) for error image.

Storing bitmaps in an array looks like a bad idea though. If you're loading images in a list, put glide call in your adapter and let glide take care of the catching itself.

gitter
  • 1,706
  • 1
  • 20
  • 32
0

Simply use this line of code in your adapter :

Glide.with(context).load(url).error(R.drawable.noimage).centerCrop().crossFade().into(ivImage);
Hamed Nabizadeh
  • 527
  • 4
  • 9
0

You can add your default place holder until images load.

Glide.with(getActivity()) 
  load(backgroundImage).placeholder(R.drawable.no_images); 

Also you can add different place holder when images failed to load in error tag.

Glide.with(getActivity()).load(backgroundImage).placeholder(R.drawable.no_images).error(R.drawable.no_images);
QuokMoon
  • 4,387
  • 4
  • 26
  • 50