Glide listener doesn't work

AndroidCallbackImageviewAndroid Glide

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

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.

Solution 5 - Android

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.

Solution 6 - Android

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)

Solution 7 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGiorgio AntonioliView Question on Stackoverflow
Solution 1 - AndroidGiorgio AntonioliView Answer on Stackoverflow
Solution 2 - Androidandroid developerView Answer on Stackoverflow
Solution 3 - AndroidRik van VelzenView Answer on Stackoverflow
Solution 4 - AndroidMehdi YariView Answer on Stackoverflow
Solution 5 - Androidm3g4tr0nView Answer on Stackoverflow
Solution 6 - AndroidAditya PatilView Answer on Stackoverflow
Solution 7 - AndroidGilad RazView Answer on Stackoverflow