Glide Does not use Cached Image - android

Hi I am using Glide to load image and after following many posts I tried to implement Glide caching using
RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
Glide.with(mContext)
.load(imageUrl)
.transition(ImageUtil.crossFadeTransition())
.apply(ImageUtil.requestOptionsForSlider())
.listener(glideRequestListner)
.into(imageView)
My knowledge is that if I use diskCacheStrategy(DiskCacheStrategy.ALL) then the above Glide image loading code will automatically use cache if available, or download if not, right?
But I see everytime the code is called, (checking my internet speed meter) that it is always fetching/downloading from the given url (even though I can see the app memory size is increasing).
I even tried this code also-
val future: FutureTarget<File> = Glide.with(mContext)
.load(imageUrl)
.downloadOnly(500, 500)
Nothing seems to work
What should I do so that Glide uses cache if available?What is wrong with my conception about the caching implementation? Any help is very much appreciated!

Are you sure it is downloading again ? did you try disabling internet just before loading the images ?
Otherwise, is your image URL always the same for the same image ? (as Glide uses it as a cache key).
You can use this in the following calls to check if images are in cache:
Glide.with(fragment)
.load(url)
.onlyRetrieveFromCache(true)
.into(imageView);

Related

Glide load thumbnail not working

I'm using Glide to load thumbnail from videos but it doesn't seem to be working on my app. The ImageView is just empty for some reason.
Glide.with(context)
.load(url)
.asBitmap()
.thumbnail(0.1f)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(target);
I added a listener, so I can figure out what is wrong but the Exception thrown is null.
I tried using ThumbnailUtils using the same url because I thought there might be something wrong with it but the thumbnail loads fine.
Anyone experiencing the same? I'm using a Nexus 7 (6.0.1)
Even i stumbled upon the same situation. Somehow from the uri its not loading the image unless create a file instance using local file path. So to make it work i used it like below
Glide.with(mContext).load(Uri.fromFile(new File(path)).into(icon);
In the doc they are using the same approach. You can refer here : Glide - Videos
Apart from that i also noticed unusual behaviour of using the cache. If you are using cache strategy as DiskCacheStrategy.ALL or DiskCacheStrategy.SOURCE it doesn't load the thumbnail but if i am using DiskCacheStrategy.RESULT it works. Hope it helps
As explained in Glide documentation, this feature is only available for videos stored locally on the device.
Also, you should use a path like /storage/emulated/0/Pictures/example_video.mp4. Adding file:/// before this path won't work out either.
You can find more informations here : https://futurestud.io/blog/glide-displaying-gifs-and-videos
Cheers !
You can use override, which surely works:
Glide.with(context)
.load(url)
.crossFade()
.override(width, height)
.into(imageView);
Glide.with(mcontext)
.applyDefaultRequestOptions(RequestOptions.centerCropTransform()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE))
.load(videourl)
.into(thumbnailimg);
Try using this code
#BindingAdapter("videoThumbnailFromUrl")
fun setImageFromVideoUrl(imageView: ImageView, url: String) {
val thumb = 10000L
val options = RequestOptions().frame(thumb)
Glide.with(imageView.context).load(url).apply(options).placeholder(R.drawable.place_holder).into(imageView)
}

How to invalidate Glide cache for some specific images

I'm writing an app which needs to load a lot of images from the internet (a manga reader). I need to cache some thumbnail images for offline use, any others should be cleared when app closed.
I read some about cache invalidation on Glide page, they said the best way is to change the content url, but how Glide know if it is a modified url of old content or a new one? I'm new to Glide here.
https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
Thank in advance :)
You can use following to load image:
Glide.with(context)
.signature(new StringSignature(yourVersionMetadata))
.into(imageView)
Just change yourVersionMetadata when you load image and it will not load from cache if yourVersionMetadata is different.

Android - use picasso to load image without storing it in cache

I want to use picasso to load an image from a url into a placeholder, but not store that image in cache - in other words, I want the image to be downloaded from the net directly to disk and then loaded from disk when needed. I understand there's a class called RequestCreator where you can specify memory policy - does anyone have an example of using picasso/requestcreator to do something like this?
So.. something like:
RequestCreator requestCreator = new RequestCreator();
requestCreator.memoryPolicy(MemoryPolicy.NO_CACHE);
....
merged with:
Picasso.with(context).load(someurl).fit().placeholder(someplaceholder).into(sometarget)..
Picasso supports this by it's skipMemoryCache() in the Picasso builder. An example is shown below.
Picasso.with(context).load(imageUrl)
.error(R.drawable.error)
.placeholder(R.drawable.placeholder)
.skipMemoryCache()
.into(imageView);
With the new API you should use it like this so that it skips looking for it and storing it in the cache:
Picasso.with(context).load(imageUrl)
.error(R.drawable.error)
.placeholder(R.drawable.placeholder)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageView);
NO_CACHE
Skips memory cache lookup when processing a request.
NO_STORE
Skips storing the final result into memory cache. Useful for one-off requests to avoid evicting other bitmaps from the cache.
For picasso:2.71828 or above version use the following for skipping using disk cache networkPolicy(NetworkPolicy.NO_CACHE) :
Picasso.get()
.load(camera_url)
.placeholder(R.drawable.loader2)
.networkPolicy(NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE)
.into(img_cam_view);
Picasso 2.5.0
If you are using Picasso to load image from Internet, you have to use NetworkPolicy attribute.
.networkPolicy(NetworkPolicy.NO_STORE)
but live memory cache (Not disk cache) is useful, you might want to keep it.
just append this at the end of url.
"?=" + System.currentTimeMillis();

Picasso: Loading cached image from disk too much slow

So, I was starting my project and wants to use Picasso in my project because its popular and used by many projects out there.
I included picasso using gradle and tried loading facebook profile url with this. http://graph.facebook.com/rohitiskul/picture.
It worked very well. It loaded image from network without any issues. I restarted the app.(Without actually killing the process). It showed me the same image instantly cached in Memory.
But then, I killed the app (force stop) and restarted. It took almost 10+ seconds to load the image. And that image was loading from the disk when I checked in the debug logs.
My code looks like this -
In MainActivity-
Picasso.with(context)
.load("http://graph.facebook.com/rohitiskul/picture")
.into(imageView);
In application class-
Picasso picasso = new Picasso.Builder(this)
.indicatorsEnabled(true).loggingEnabled(true).build()
Picasso.setSingletonInstance(picasso);
Anyone with the similar problem? Any solution would be helpful.
I tried loading same Url with UniversalImageLoader and it was fast when fetching cached image from disk.
Edit
Earlier while playing with my app, I found out that Picasso wasn't loading the disk cached image when device was offline.
I encounter the same problem,
but find only slow for the first image, later images will be fast.
Probably it needs a warm-up (loading index cache) ?
Okay i got your problem. I have fixed it by doing this
Picasso.with(context)
.load("http://graph.facebook.com/rohitiskul/picture")
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView, new Callback() {
#Override
public void onSuccess() { }
#Override
public void onError() {
// Try again online if cache failed
Picasso.with(context)
.load("http://graph.facebook.com/rohitiskul/picture")
.into(imageView);
}
});
Explanation:
Picasso will look for images in cache.
If it failed only then image will be downloaded over network. In your case from facebook.
This issue I had also faced earlier, with this what I understood that Picasso refer the cached image based on image name mentioned in the URL.
In your case you don't have image name in URL like 'image1.jpg'. Due to which Picasso is finding it difficult to read from cache and it downloads the image everytime
You can give a try to image containing the image name in URL and that will work
Picasso doesn't offer disk cache out of the box. Instead, it relies on an Http Cache.
Make sure you add OkHttp to your dependency list.
Add a string identifier with the stableKey method when making the request so Picasso can identify your requests and quickly load it from the cache.
Example:
Picasso.Builder(context).loggingEnabled(true).build()
.load(imageUrl)
.stableKey("myImage")
.into(imageView)

How to inform Picasso that the image in the link changed?

im using picasso library to load images.
However in my application, users can have profile pictures and the link for the image is constant... Picasso has no clue that the image has changed...
I tried using : .skipMemoryCache() but it wasn't an ideal solution...
Is there a way to check if there's a new picture in the same link using picasso? Thanks!
Apparently, there is no API in the current (2.3.2) version of Picasso to achieve this (but it is a work in progress - see this bug).
That aside, if you have control of the server side, you may want to think about your design decision to provide the changing profile picture at a constant URL.
An alternative would be: Include the current profile picture URL in the profile information you retrieve. This way, your cache can use the cached image - and as soon as the profile information provides a new URL, Picasso will fetch it. In all other cases, Picasso can leverage the cache.
I got the answer from this link
change your URL as shown below:
String imageurl = url + "?time=" + System.currentTimeMillis();
Picasso.with(getContext()).load(imageurl).into(imageView);
this worked for me. thanks
Picasso.with(context)
.load(url)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.fit()
.placeholder(YOUR PLACE HOLDER RESOURCE)
.centerCrop()
.into(imageView);
One solution is to invalidate the cache like so
Picasso.with(context).invalidate(imagePath);
The other way to force download the image which then is cached for subsequent use as mentioned in the code.
Picasso.with(context)
.load(imagePath)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(userAvatar);

Categories

Resources