4

I am using glide for load image in my application. Its working fine with my condition like

if(image_enabled==1){
Glide.with(getContext()).load(constant.SERVER_URL+"images/"+quoteData.get(KEY_PICTURE).apply(myOptions).into(mImageView);
}

else if(image_enabled==0){
Glide.with(getContext()).load(constant.SERVER_URL+"images/"+quoteData.get(KEY_PICTURE)).apply(myOptions).into(mImageView);
}

But I want load one more url if any above condition failed to load image. I do not know which method is for track load failed in glide. Let me know if any one can help me for get it. Thanks

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
Priyanka
  • 137
  • 2
  • 7
  • Possible duplicate of [Glide - call method after fallback or error when trying load photo](https://stackoverflow.com/questions/38250802/glide-call-method-after-fallback-or-error-when-trying-load-photo) – AskNilesh Feb 15 '18 at 07:54

2 Answers2

7

I think this will help you. Just set your url in .error() it will load on failure.

Glide.with(getContext())
      .load("your url")
      .error("your default drawable")
      .into(mImgProfile);

Or else you can use below as well

Glide.with(mActivity)
     .load("your url")
     .listener(new RequestListener<String, GlideDrawable>() {
         @Override
         public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            return false;
         }

         @Override
         public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                return false;
         }
       })
     .into(mImgProfile);
Maradiya Krupa
  • 213
  • 1
  • 6
0

Here completely workable code for load another url if current load failed:

Small extension for code reuse:

fun RequestBuilder<Drawable>.setListener(
    onReady: () -> Boolean = { false },
    onFailed: () -> Boolean,
): RequestBuilder<Drawable> {
    return listener(object : RequestListener<Drawable> {
        override fun onLoadFailed(
            e: GlideException?,
            model: Any?,
            target: Target<Drawable>?,
            isFirstResource: Boolean,
        ): Boolean = onFailed()

        override fun onResourceReady(
            resource: Drawable?,
            model: Any?,
            target: Target<Drawable>?,
            dataSource: DataSource?,
            isFirstResource: Boolean,
        ): Boolean = onReady()
    })
}

Code example from ViewHolder:

private val avatarImage = view.findViewById<ImageView>(R.id.avatar_image)

private val glideHandler = Handler(Looper.getMainLooper())

fun bind(urlList: List<String>) {
    if (urlList.isNotEmpty()) {
        loadImage(urlList)
   }
}

fun unbind() {
    glideHandler.removeCallbacksAndMessages(null)
}

private fun loadImage(urlList: List<String>, index: Int = urlList.indices.first) {
    val url = urlList.getOrNull(index) ?: return

    Glide.with(avatarImage)
        .load(url)
        .placeholder(R.drawable.img_holder_load)
        .error(R.drawable.img_holder_error)
        .setListener {
            val newIndex = index + 1

            if (newIndex in urlList.indices) {
                glideHandler.post { loadImage(urlList, newIndex) }
                return@setListener true
            }

            return@setListener false
        }
        .into(avatarImage)
}
SerjantArbuz
  • 982
  • 1
  • 12
  • 16