20

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?

Adinia
  • 3,722
  • 5
  • 40
  • 58
gmmo
  • 2,577
  • 3
  • 30
  • 56
  • don't call load method in separate thread or make that thread run onUi thread – Vivek Mishra Feb 26 '16 at 18:41
  • can you elaborate more? How can I make it not run on a onUI thread? I need to pass the ImageView pointer to glide. This pointer is retrived from the main UI thread. – gmmo Feb 26 '16 at 19:08

7 Answers7

13

Update image in main ui thread

runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(MainActivity.this)
                                    .load("image URL")
                                    .into(imageView);
                        }
                    });
Raj Joshi
  • 2,669
  • 2
  • 30
  • 37
10

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.

Muhammad Waleed
  • 2,517
  • 4
  • 27
  • 75
mblcdr
  • 762
  • 9
  • 18
6

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);
hushed_voice
  • 3,161
  • 3
  • 34
  • 66
1

Here is the Kotlin-way solution

Glide.with(context).asBitmap().load(photo_url).into(
      BitmapImageViewTarget(imgYourResourceID)
)
Berkay92
  • 552
  • 6
  • 21
0

In my case i want to show notification from FirebaseMessagingService with image which will be downloaded via Glide, what gave me success is this 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();
                }
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
0

I need to decrypt and display large images in a recyclerView And because it was a heavy process, I had to do it in the background, so that's why :

 public void decryptImage(Uri uri, ViewHolder holder) {

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {

                Glide.with(G.context)
                        .asBitmap()
                        .load(decrypt(G.getEncryptionKey(), G.getConstantKey(), uri.getPath()))
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                                Glide.with(G.context).load(resource).into(holder.mImageView);
                            }

                        });
    
        }
    };
    new Thread(runnable).start();


}
A.Hosein
  • 182
  • 1
  • 10
-2
 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();
    }
}