Home > OS >  Glide loadFailed donothing
Glide loadFailed donothing

Time:07-05

My imageView has default picture

then I load a url with Glide

I want:

if Glide load success, set picture to imageView

if Glide load fail, do nothing (imageViewe still show the default picture)

        Glide.with(myImageView)
            .load("123456")
            .listener(object : RequestListener<Drawable> {
                override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                    return false
                }

                override fun onl oadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                    return false
                }
            })
            .into(myImageView)

but when Glide load fail, it make myImageView default picture disappear

even I change the onl oadFailed return value to true it still not working

how can I maek Glide donothing when onLoadFailed

CodePudding user response:

Don’t use into if you need to change the image via the onResourceReady callback.
Use submit like:

Glide.with(myImageView)
    .load("123456")
    .listener(object : RequestListener<Drawable> {
        override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
            if (resource != null) imageView.setImageDrawable(resource)
            return false
        }

        override fun onl oadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
            return false
        }
    })
    .submit()

CodePudding user response:

Try below, use error() method to handle request failure

Glide.with(context)
    .load("http://via.placeholder.com/***.png")
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.imagenotfound)
    .into(ivImg);

Refer https://guides.codepath.com/android/Displaying-Images-with-the-Glide-Library

  • Related