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

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

Has 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:

https://github.com/bumptech/glide/issues/310

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:

    Glide.get(mContext);     loadUserImage(userImageUrl);      // 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() {             @Override             public void run() {                 if (!mUserImageLoaded) {                     loadUserImage(userImageUrl);                 }             }         }, 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>() {              @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) {                 mImageMessageContent.invalidate();                 mUserImageLoaded = true;                 return false;             }         }).into(mImageMessageContent);      } else {         mImageMessageContent.setVisibility(View.GONE);     } } 

and mContext is just the activity "this" pointer.

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

回答1:

The into(ImageView) method of Glide requires you to call it only on main thread, but when you 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) {       @Override       protected void setResource(Bitmap resource) {        //Play with bitmap         super.setResource(resource);       }     }); 

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



回答2:

Posting the code just in case it helps someone.

Bitmap myBitmap = Glide.with(applicationContext)         .load(yourUrl)         .asBitmap()         .centerCrop()         .into(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL)         .get() imageView.setImageBitmap(myBitmap); 


回答3:

Update image in main ui thread

runOnUiThread(new Runnable() {                         @Override                         public void run() {                             Glide.with(MainActivity.this)                                     .load("image URL")                                     .into(imageView);                         }                     }); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!