1

I've searched for a solution, but all I found are 100000 questions about how to use a placeholder which is not what I need.

I need Glide to try to load an image from another url if the first one failed.

This is what I've tried:

        fun tryAlternative(){

            val alternativeUrl = func.getMediaUrl(mediaServer) + mediaParameters

            Glide
                .with(context)
                .load(alternativeUrl)
                .listener(object : 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 {
                        holder.picLoadIcon.visibility = View.GONE
                        holder.image.requestLayout()
                        holder.picContainer.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
                        holder.picContainer.requestLayout()
                        return false
                    }
                })
                .apply(glideOptions)
                .fitCenter()
                // .transform(CutOffLogo())
                .into(holder.image)
        }

        Glide
            .with(context)
            .load(fileUrl)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    Handler(Looper.getMainLooper()).postDelayed({
                        tryAlternative()
                    }, 500)

                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    holder.picLoadIcon.visibility = View.GONE
                    holder.image.requestLayout()
                    holder.picContainer.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
                    holder.picContainer.requestLayout()
                    return false
                }
            })
            .apply(glideOptions)
            .fitCenter()
            // .transform(CutOffLogo())
            .into(holder.image)

It works on my test devices which are android 8 and 11.

But some of the users are getting a crash, including api 8 and 11.

This is the error:

java.lang.IllegalArgumentException: 
  at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed (RequestManagerRetriever.java)
  at com.bumptech.glide.manager.RequestManagerRetriever.get (RequestManagerRetriever.java)
  at com.bumptech.glide.manager.RequestManagerRetriever.c (RequestManagerRetriever.java:18)
  at com.bumptech.glide.Glide.with (Glide.java)
  at de.bla.pname.adapters.MemesAdapter.onBindViewHolder$tryAlternative (MemesAdapter.java)
  at de.bla.pname.adapters.MemesAdapter.access$onBindViewHolder$tryAlternative (MemesAdapter.java)
  at de.bla.pname.adapters.MemesAdapter$onBindViewHolder$10.onLoadFailed$lambda-0 (MemesAdapter.java)
  at de.bla.pname.adapters.MemesAdapter$onBindViewHolder$10$$InternalSyntheticLambda$1$c05fc0ac0dc127b19ad39a5f25d91eeedfcc97ef8006ab4a8bfc1edb23485f8f$0.run$bridge (MemesAdapter.java)
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:246)
  at android.app.ActivityThread.main (ActivityThread.java:8587)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)

How to do this correctly?

1 Answer 1

0

you can do this without Handler.

use just this tryAlternative() instead of

Handler(Looper.getMainLooper()).postDelayed({
                        tryAlternative()
                    }, 500)
Sign up to request clarification or add additional context in comments.

5 Comments

I know but you think handler is the cause of crash?
i think . may be
I increased to 5000 to see if I get any crashes but no I don't so I don't think that this is the problem
without handler you can use Coroutine and you can check your valid image url if url is valid then you through the url to the glide
They are valid, the only problem is 500 error when the server is overloaded. Checking if the image can be loaded before actually doing it will put extra workload to the servers, so it's not an option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.