gmmo gmmo - 1 year ago 550
Android Question

Glide assert: java.lang.IllegalArgumentException: You must call this method on the main thread

anyone used glide to fetch images from a background thread? I keep getting this assert:

java.lang.IllegalArgumentException: You must call this method on the main thread

but according to this thread, it should work (i think):

yet, I cannot get it to work, unless I call it from the main thread.

Here's is what I am trying to do from the main thread:


// wait 5 seconds before trying again
int imageLoadingTimeOut = mContext.getResources().getInteger(R.integer.image_loading_time_out);
if (imageLoadingTimeOut > 0) {
new Timer().schedule(new TimerTask() {
public void run() {
if (!mUserImageLoaded) {
}, imageLoadingTimeOut);

and the loadUserImage:

private boolean mUserImageLoaded = false;

private void loadUserImage(String userImageUrl) {

if (userImageUrl != null && !userImageUrl.isEmpty() && !mUserImageLoaded) {
Glide.with(mContext).using(Cloudinary.getUrlLoader(mContext)).load(userImageUrl).crossFade().listener(new RequestListener<String, GlideDrawable>() {

public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;

public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
mUserImageLoaded = true;
return false;

} else {

and mContext is just the activity "this" pointer.

Anyway, can I use Glide from a thread different than main?


Answer Source

The into(ImageView) method of Glide requires you to call it only on main thread, but when your pass the loading to a Timer it will be executed in a background thread.

What you can do is to retrieve a bitmap by calling get() instead of into() and then set that bitmap on the ImageView by calling setImageBitmap().

Glide.with(getApplicationContext()).load("your url").asBitmap() .into(new

BitmapImageViewTarget(imgView) {
 protected void setResource(Bitmap resource) {
   //Play with bitmap

You can also take a look at this document for more information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download