Glide assert: java.lang.IllegalArgumentException: you must call this method in the main thread

Has anyone used Glide to get images from a background thread? I keep getting this statement:

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

but according to this thread, this should work:

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

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

Here 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 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 a pointer to the "this" action.

In any case, can I use Glide from a thread other than the main one?

+14
source share
5 answers

The into(ImageView) method in Glide requires that you only call it in the main thread, but when you pass the load to Timer, it will execute in the background thread.

What you can do is get the bitmap by calling get() instead of into() and then set bitmap to 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 read this document for more information.

+8
source

Posting 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); 
+6
source

Refresh image in ui main topic

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

In my case, I want to show a notification from the FirebaseMessagingService with an image that will be uploaded via Glide , which gave me success, this is a piece of code:

 try { Bitmap bitmap= Glide.with(getApplicationContext()) .load(imageUrl) // image url in string .asBitmap().into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get(); // now i can pass bitmap to notificationBuilder like notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap)); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } 
0
source
  private class AsyncTaskRunner extends AsyncTask<String, String, RequestBuilder> { ProgressDialog progressDialog; @Override protected void onPreExecute() { progressDialog = ProgressDialog.show(MainActivity.this, "Please Wait", "Image is loading..."); } @Override protected RequestBuilder<Drawable> doInBackground(String... strings) { URL url = null; try { url = new URL(strings[0]); } catch (MalformedURLException e) { e.printStackTrace(); } RequestBuilder<Drawable> g= Glide.with(MainActivity.this).load(url); return g; } @Override protected void onPostExecute(RequestBuilder v) { v.into(imageView); imageView.setVisibility(View.VISIBLE); progressDialog.dismiss(); } } 
0
source

All Articles