Android Universal Image Loader: by-pass disc/memory cache check when loading an image?
Asked Answered
H

1

0

The library only exposed option to enable/disable caching in display options. Is there a way to avoid disc or memory cache check when loading images?

My problem is that the library always check the disc cache first. My disc cache is set with small thumbnail size. When I need to load images for screen size, I cannot skip the disc cache , it always get me the lower resolution image.

Harney answered 22/2, 2014 at 22:31 Comment(5)
Do thumbnail image and full-sized image have the same URLs?Hance
@NOSTRA Hi, thanks for your great library. I use UIL for local image caching. My usage is described here: https://mcmap.net/q/14810/-local-image-caching-solution-for-android-square-picasso-universal-image-loader-glide-fresco/727768 I have all local images on file system, but they're huge, so I want to use disc cache for tiny thumbnails in grid view. (InSampleSize option is helpful for memory footprint, but loading process has to scan several MB on disk)Harney
@NOSTRA There's a loadImage with ImageSize option, but the library does not respect that, it fetch the disc cache version no matter of what size it is.Harney
If the UIL memory can do multiple size caching, it would be nice to extend this to disc cache, maybe by default off. This can be very useful for people want to do multiple level of thumbnail caching.Harney
Ok, I understand your case.Hance
H
2

UIL always check caches before displaying. So there is no way to avoid without changins the sources.

But I think you can solve your problem by extending disk cache. The goal is to return non-existing file from get() method when you want to load full-sized image. So you should do as usual to load thumbnails. When you need to display full-sized image you should disable caching on disk in display options (DisplayImageOptions) and then do something like this:

((MyDiscCache) imageLoader.getDiscCache()).setIgnoreDiskCache(true);

So your cache must return any non-existing file (but not null).

When you return to display thumbnails you should "enable" (setIgnoreDiskCache(false)) disk cache back.

UPD: Create your own disk cache and set it to config.

public class MyDiscCache extends UnlimitedDiscCache {

    private boolean ignoreDiskCache;

    public MyDiscCache(File cacheDir) {
        super(cacheDir);
    }

    public MyDiscCache(File cacheDir, FileNameGenerator fileNameGenerator) {
        super(cacheDir, fileNameGenerator);
    }

    @Override
    public File get(String key) {
        if (ignoreDiskCache) {
            return new File("fakePath");
        } else {
            return super.get(key);
        }
    }

    public void setIgnoreDiskCache(boolean ignoreDiskCache) {
        this.ignoreDiskCache = ignoreDiskCache;
    }
}
Hance answered 22/2, 2014 at 23:31 Comment(6)
Thanks, I'll try it out. In the future, do you think it's worth adding the following features: 1. Option to by-pass disc and/or memory cache checking. 2. Option to request specific size, and if cached size is smaller, load from the original file. 3. Cache multiple sizes on disc, just like the memory cache. (Again thanks for providing this great library. I use SDWebImage on my iOS app, and now am porting the app to Android. I tried Picasso first, but it's limited in many ways.)Harney
I looked again, your suggestion does not work. DiscCache interface specify get() with only a string key argument. There's no way I could inform the implementation to behave differently. I cannot put special string in the key, that will also affect other things.Harney
Your URL should be the same. You just need to on/off disk cache. Updated the answer.Hance
Thanks for your answer! BTW, "disc" should really be "disk" support.apple.com/kb/ht2300 :)Harney
Yes, I know :) It's historical mistake.Hance
I didn't use your solution. I ended up changing the source code: I'm using the memory key for disk cache, so that it's also size sensitive. There's very inefficient steps in UIL: if the source file is local, and the disk cache resize extra is enabled, UIL still copy the source file to cache disk, which might be several MBs, and then do the resize. File move can be saved in this special case.Harney

© 2022 - 2024 — McMap. All rights reserved.