Incorrect image rendered with Glide library
Asked Answered
T

5

13

I am using the glide library in my android project to fetch and display images. Earlier I was using version 2.0.5 and facing rendering issue. The problem was that the wrong images were rendering. I have updated library to 3.3 version and it now crashes with the following exception.

14-Sep-2014 08:41:31 PM java.lang.IllegalArgumentException: 
You cannot start a load for a destroyed activity
    at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:63)
    at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:29)
    at com.bumptech.glide.Glide.with(Glide.java:537)
    at com.miamiheat.common.MHImageDownloadWrapper.loadImage(MHImageDownloadWrapper.java:12)
    at com.miamiheat.ui.module.MHWallpaperModule.setWallpaperViewData(MHWallpaperModule.java:234)
    at com.miamiheat.ui.module.MHWallpaperModule.taskManagerResponseCallback(MHWallpaperModule.java:257)
    at com.miamiheat.service.taskmanager.MHWallpaperTaskManager.asyncResultCallback(MHWallpaperTaskManager.java:133)
    at com.miamiheat.service.framework.MHAsyncServiceTask.onPostExecute(MHAsyncServiceTask.java:191)
    at android.os.AsyncTask.finish(AsyncTask.java:631)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    at dalvik.system.NativeStart.main(Native Method)

Is there anyway to cancel the image download request on activity destroy.

Terresaterrestrial answered 13/9, 2014 at 12:39 Comment(1)
I have solved using picasso instead of glide.Check hereRevocable
T
4

You can always use Glide.clear() to cancel a load.

However, that exception occurs when you try to start a new load after your activity has been destroyed. Are you starting to load images after fetching some data asynchronously? If so you probably want to cancel your async fetch when your activity is stopped or at least ignore the result if the fetch finishes and your activity is destroyed.

Trope answered 15/9, 2014 at 16:0 Comment(2)
Hi, I am using Glide.with(context).load(imageUrl).animate(R.anim.fade_in).into(imageView); to load image. It is getting loaded by the list item layout using list adapter.Terresaterrestrial
In Glide v4 it is: Glide.with(fragment).clear(target) (see the migration guide)Gibby
O
7

Glide.clear() sometimes doesn't help for me. Pass application context to Glide.with(...) using getApplicationContext() method.

Offshore answered 1/9, 2015 at 22:38 Comment(1)
In my case, it was on screen rotation. Your suggestion helped me. Thanx.Schauer
T
4

You can always use Glide.clear() to cancel a load.

However, that exception occurs when you try to start a new load after your activity has been destroyed. Are you starting to load images after fetching some data asynchronously? If so you probably want to cancel your async fetch when your activity is stopped or at least ignore the result if the fetch finishes and your activity is destroyed.

Trope answered 15/9, 2014 at 16:0 Comment(2)
Hi, I am using Glide.with(context).load(imageUrl).animate(R.anim.fade_in).into(imageView); to load image. It is getting loaded by the list item layout using list adapter.Terresaterrestrial
In Glide v4 it is: Glide.with(fragment).clear(target) (see the migration guide)Gibby
W
3

You must call Glide.with(getActivity()).pauseRequests() or Glide.get(getActivity()).clearMemory() in the method onDestroy() or onDestroyView(), just delete it.

Waldheim answered 22/3, 2016 at 10:18 Comment(0)
S
0

add this before Glade call

if(!this.isDestroyed()){
   Glide.with(activity).load(...)
   ....
}
Salvidor answered 25/5, 2017 at 7:59 Comment(1)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others.Silent
L
0

Make sure you clear your view at the beginning of the onBindViewHolder method. That's all.

@Override
public void onBindViewHolder(final Adapter_Feed_Msg.ViewHolder holder, final int position) {

    Glide.with(mContext).clear(holder.ProfileImage);
    Glide.with(mContext).clear(holder.postImage);

 .
 .
 .
}
Labonte answered 12/6, 2021 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.