Android Ion Cache Refresh - android

I'm using a specific url to obtain an image online, but the url itself automatically changes every few minutes.
I'm using ION library from Here
The problem I'm having is when I refresh the page, the page itself looks like it is refreshing but the exact same picture is appearing.
I'm assuming its a cached image? If I reinstall the application, then it obtains the correct image again.
This is how I'm using ION where imageID2[position] is just a typical url to a jpg.
Ion.with(imageView)
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_launcher)
.load(imageId2[position]);
Is there anyway I can disable the cache or just make it refind the images again?

Use .noCache() to bypass caches.

As per documentation here, use the "long" way to build an ImageView request so that you can add headers, noCache, etc.
Change your original request from this:
Ion.with(imageView)
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_launcher)
.load(urlString);
To this:
Ion.with(context)
.load(urlString)
.noCache()
.withBitmap()
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_launcher)
.intoImageView(imageView);

You can append a unique string to your url for example a current timestamp in order to force Ion to treat each request as unique.

Related

Picasso download random image

in my new app, at some point I need to fetch a random image (i use this link) using Picasso. The problem is even if I set the network policy to no cache the image stays the same if I exit the activity and relaunch it.
Note it's the second activity that contains this Picasso downloading
Refer this its well explained here!
Summarizing: Use NetWorkPolicy along with MemoryPolicy
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[2])
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(imageViewFromNetwork);
Try adding a random value to your url.
Example :
http://unsplash.it/200/?random=<a random value computed each time>

Profile Image Uploader and Load it with Picasso

I have an android app which has profile image.
The user can edit his/her profile image.
I'm using Picasso to load images, but every time I load profile image, Picasso loads old image.
When I upload profile image I don't change it's url, so profile image url for every user is constant.
First question: should I change profile image url every time user change profile image?
Second question: When user change his/her profile image I invalidate Picasso to load new image using this code:
picassoInstance.load(url)
.networkPolicy(NetworkPolicy.NO_CACHE)
.transform(new CircleTransform())
.placeholder(dr)
.into(imageView);
But Picasso show me the old one.
And one more thing:
I load image url into imageView without calling networkPolicy method at first.
How should I handle this problem?
Every Image has different name, whenever new image uploaded you have to update your image Url
Eg. current url : https://photographylife.com/nikon-d810-high-resolution-image-samples
where Image name is : nikon-d810-high-resolution-image-samples
and after uploaded new image, name will be different for example : image-sample
so you need to update that image Url : https://photographylife.com/image-sample with new name
try this, i telling you cause im doing the same hope this will help you!
So lets just discuss one by one:
Should I change profile image url every time user change profile
image?
No. You might think of other ways around. In my case, I download the picture each time the url changes with a specific name of the user (as we're considering profile picture). So when I loaded the image with Picasso I passed the file path instead of the URL so that, it won't fetch from the URL each time. Picasso doesn't do that either actually. It maintains a cache. If the file doesn't exist, just put a placeholder.
When user change his/her profile image I invalidate Picasso to load
new image using this code:
I don't see any invalidate code here in your code. I would refer to this link to see how they've solved the cache problem.
I load image url into imageView without calling networkPolicy method
at first.
So if you fetch the image from the external storage, I don't think there's a necessity to do so.
And another suggestion is to use Glide in these cases. This is almost the same thing as Picasso, but personally I like to use Glide instead of Picasso.
Hope that helps.
Using Glide instead of Picasso because Glide recommended by google
http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
Preventing for caching in Glide
Glide.with(DemoActivity.this)
.load(Uri.parse("file://" + imagePath))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(mImage);
Add this class to the com.squareup.picasso package.
package com.squareup.picasso;
public class PicassoTools {
public static void clearCache (Picasso p) {
p.cache.clear();
}
}
Because cache has package visibility, this util class can clear the cache for you. You just have to call it:
PicassoTools.clearCache(Picasso.with(context));
I find where problem is:
I turned down disk cache but not memory cache. I should use this code:
picassoInstance.load(url)
.networkPolicy(NetworkPolicy.NO_CACHE)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.transform(new CircleTransform())
.placeholder(dr)
.into(imageView);
I use this link to:
picasso-influencing-image-caching
Thanks everybody
You need to invalidate the file before loading use the code given below:
Picasso.with(getActivity()).invalidate(file);
To know more details kindly study the Picasso documentation from their website.

Android Image invalidate in fresco

I am migrating android image caching library from picasso to fresco. I want to know if there is any way to invalidate image already catched as I am adding feature to replace existing image there is way to do so in picasso like
Picasso.with(context).invalidate(URI);
This line remove the cached image and use new one using the url provided which is same like,
http://example.com/image_path
In fresco I have tried using
Fresco.getImagePipeline().evictFromMemoryCache(uri);
This is removing image from view but adding same old cached image again and not getting new one from network as it is working in picasso.
Please refer question Invalidate cache in Picasso The accepted answer doing great in case of picasso.
Fresco.getImagePipeline().evictFromMemoryCache(uri);
Above code line remove the image from the catche but image remains there in the disk and render same if called. We need to remove same image from disk as well. Bellow two lines remove the the image from disc cache also we need to remove the small that is thumbnail image if saved from disk cache.
Fresco.getImagePipelineFactory().getMainDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
Fresco.getImagePipelineFactory().getSmallImageDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
Note: if you are using custom cache key you need to change it that way.
Try this
public static void clearCache(){
//
ImagePipeline imagePipeline = com.facebook.drawee.backends.pipeline.Fresco.getImagePipeline();
imagePipeline.clearMemoryCaches();
imagePipeline.clearDiskCaches();
// combines above two lines
imagePipeline.clearCaches();
}

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