8

I've been working on this for a while now and it is time to ask a SO question on it.

Find and load cached Image in Glide looks like it, but following the links and the discussion on Glide's page it doesn't work for me.

On Activity A, I load the image via an URL in an ImageView.

After that, I go offline with the device. Then I open Activity B, where I load the same URL in a different ImageView, which has other dimensions. It only works if the width and height are equal to the ImageView on Activity A, or you supply an override() with the width and height of that first ImageView. But Activity B does not know about that.

I do this in Activity A:

Glide.with(context)
    .load(url)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .placeholder(R.drawable.placeholder)
    .crossFade()
    .into(image);

and in activity B I do:

Glide.with(mContext)
    .load(url)
    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
    .into(imageView);

I tried the above, because I thought the disk strategy will store the original too, so it will load that one than. Result is that no image is loaded.

I am thinking of going back to Picasso, because I think this is possible there, but I give Stackoverflow one more try.

I would throw in a bounty right away if that was possible...

Community
  • 1
  • 1
Boy
  • 7,010
  • 4
  • 54
  • 68

2 Answers2

9

Can you try the following code for Activity B?

Glide.with(mContext)
   .load(url)
   .diskCacheStrategy(DiskCacheStrategy.SOURCE)
   .into(imageView);
ap6491
  • 805
  • 1
  • 10
  • 26
  • 1
    WTH, this seems to work! But why? I though the cache strategy only applied to downloading / which image resolution is saved, not which one is loaded. Can you explain? – Boy Jan 22 '16 at 19:50
  • DiskCacheStrategy.SOURCE saves the original data to cache. – ap6491 Jan 22 '16 at 19:59
  • I understand that it would do that while downloading. But I have no internet connection when running this code, so the caching is not applicable: there is nothing to download. Or does it try to save the original only, which doesn't work because of lack of internet, and then resizes the original (already stored) to the width and height of the ImageView? – Boy Jan 22 '16 at 20:08
  • That is true. It resizes the original (already stored) to the size of the ImageView. Please refer to the Disk Caching section at the following link: http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en – ap6491 Jan 22 '16 at 20:19
  • But I'd expect that if I use DiskCacheStrategy.ALL in Activity A, the original size is already stored. So why doesn't Glide by default use this original size? – Boy Jan 22 '16 at 20:30
  • 1
    DiskCacheStrategy.ALL caches all versions of the image which is the default behaviour. However, DiskCacheStrategy.SOURCE is used when you know you are going to manipulate and make a bunch of versions. – ap6491 Jan 22 '16 at 20:59
0

now you can use that :

    Glide.with(MainActivity.context)
            .applyDefaultRequestOptions(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
            .load(url)
            .into(img);
Hoby
  • 1,024
  • 12
  • 24