13

I want to use Glide to eagerly download images and cache them on disk for a future use. I want to call this functionality from a background thread.

I've read Glide's caching documentation, but it doesn't explain how to download the image without having an actual target right now. Then I found this issue and tried to use a similar approach, but whatever I try I get this exception:

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

So, how can I tell Glide to cache an image from a background thread?

EDIT: I really want to call Glide's methods on background thread. I know that I can use Handler and other ways to offload it to UI thread, but that's not what I'm asking.

Vasiliy
  • 16,221
  • 11
  • 71
  • 127
  • Why don't you download ImageURLs and store it in Room DB and whenever you want, you can load the images from that DB instead. – Ümañg ßürmån Sep 23 '18 at 09:14
  • @UmangBurman, storing images in DB is a bad idea (just like storing any other big binary objects). I could download the image, store it locally and keep the local URI, but that would amount to reinventing the wheel. First I want to know whether Glide can do that. – Vasiliy Sep 23 '18 at 09:18

4 Answers4

16
GlideApp.with(context)
    .downloadOnly()
    .diskCacheStrategy(DiskCacheStrategy.DATA) // Cache resource before it's decoded
    .load(url)
    .submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
    .get() // Called on background thread
veritas1
  • 8,740
  • 6
  • 27
  • 37
3

If want to load images in cache for future use in background thread then Glide have this functionality

here how you can do this

 //here i passing application context so our glide tie itself with application lifecycle

FutureTarget<File> future = Glide.with(getApplicationContext()).downloadOnly().load("your_image_url").submit();

now if you want to retrieve the saved path that you want to store in your DB then you can do

File file = future.get();
String path = file.getAbsolutePath();

you can also do this in just one line returning a path string like this

String path = Glide.with(getApplicationContext()).downloadOnly().load("your_image_url").submit().get().getAbsolutePath();
Ashwini Saini
  • 1,324
  • 11
  • 20
  • Thanks for your very elaborate answer. It's correct, but I decided to accept another answer because it turns out that using `DATA` cache strategy is mandatory in this case. – Vasiliy Sep 23 '18 at 11:11
0

Did I get the question right?

private class update extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
        RequestOptions options = new RequestOptions()
        .diskCacheStrategy(DiskCacheStrategy.ALL) // Saves all image resolution
        .centerCrop()
        .priority(Priority.HIGH)
        .placeholder(R.drawable.null_image_profile)
        .error(R.drawable.null_image_profile);

    Glide.with(context).load(imageUrl)
        .apply(options);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        //finish
    }
}
Trk
  • 95
  • 1
  • 12
  • Nope. I don't have `profileImage`, but even if I would have one, call to `into` on background thread throws exception. – Vasiliy Sep 23 '18 at 09:23
0

You must call this method on the main thread

Wherever you call a method to use Glide or doing something from background, run inside:

runOnUiThread(new Runnable() {
                @Override
                public void run() {

              // Here, use glide or do your things on UiThread

            }
        });

To use it inside the main thread and then error should be gone.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • Thanks, but that's not what I asked. I'm interested in calling Glide's methods from background thread, not jumping back to UI thread. – Vasiliy Sep 23 '18 at 09:27
  • Correct! I just saw your edit. But, the point is, **`Glide` effects on UI** since it's an image loading library. So, no matter what you wanna achieve, it will throws exceptions like now. – ʍѳђઽ૯ท Sep 23 '18 at 09:28