54

I'm using Glide to load images and I added a listener to know when resource is ready or if there was an error of any type:

Glide.with(mContext)
    .load(url)
    .placeholder(R.drawable.glide_placeholder)
    // use dontAnimate and not crossFade to avoid a bug with custom views
    .dontAnimate()
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .listener(new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            // do something
            return true;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            // do something
            return true;
         }
    })
    .into(mCustomImageView);

The app never runs inside onResourceReady or onException but if I remove the listener and let the async download without a callback, it runs correctly:

Glide.with(mContext)
    .load(url)
    .placeholder(R.drawable.glide_placeholder)
    // use dontAnimate and not crossFade to avoid a bug with custom views
    .dontAnimate()
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .into(mCustomImageView);

I tried also with GlideDrawableImageViewTarget instead of listener to receive callbacks but app runs inside onLoadStarted but never runs inside onLoadCleared, onLoadFailed and onResourceReady.

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70
  • You're saying the listener's onException and onResourceReady methods aren't called? Returning true from those methods will prevent the Target from being called, but they should always be called for the listener regardless. – Sam Judd Sep 10 '15 at 23:23
  • I think you need to call `submit` in order to initiate it to start loading – android developer Dec 13 '17 at 10:26

7 Answers7

43

It seems to be a bug with ImageView's visibility if it's invisible or gone. I opened an issue here: https://github.com/bumptech/glide/issues/618

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70
  • 6
    omg, I was transitioning from Picasso to Glide but this was unexpected. Glide does't call the callback if the view is still invisible :( – Dr Deo Feb 14 '19 at 07:43
29

Here's one way to do it:

        Glide.with(context).load(...)
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                        //TODO handle error images while loading photo
                        return true
                    }

                    override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                        //TODO use "resource" as the photo for your ImageView
                        return true
                    }

                }).submit()
android developer
  • 114,585
  • 152
  • 739
  • 1,270
11

Ran into same issue. Having onResourceReady return false did the trick for me.

Rik van Velzen
  • 1,977
  • 1
  • 19
  • 37
9

You just need to change the return of onResourceReady and onLoadFailed from true to false.

It works for me on glide 4.9.1.

if you look at RequestListener comments you should understand.

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
Mehdi Yari
  • 481
  • 3
  • 12
6

Ran into same issue because the width and height of my ImageView were 0,0 ( both layout_width and layout_height were set to wrap_content with no Image was set on ImageView initially). giving ImageView a default width and height solved my issue.

m3g4tr0n
  • 591
  • 7
  • 15
0

Kotlin way to use Glide with listener

Glide.with(context)
                .load(image_url)
                .listener(object : com.bumptech.glide.request.RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: Target<Drawable>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        img_product_banner.visibility = View.VISIBLE
                        return false
                    }

                }).placeholder(R.drawable.placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(img_product_banner)
Aditya Patil
  • 1,287
  • 12
  • 19
-1

This worked for me

try {
        Glide.with(context)
            .asBitmap()
            .load(characterDB.url)
            .listener(object : RequestListener<Bitmap> {
                override fun onResourceReady(
                    resource: Bitmap?,
                    model: Any?,
                    target: Target<Bitmap>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }

                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Bitmap>?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }
            }
            )
            .submit()
    }
    catch (e : Exception)
    {
        Log.d("Excepion",e.message.toString())
    }
Gilad Raz
  • 147
  • 1
  • 2