Android Image invalidate in fresco - android

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();
}

Related

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.

Picasso Caching Doesn't Appear to be working

I am using Picasso to handle image loading and caching in my Android Udacity project and I am noticing the caching is not working as I'd expect:
As you can see on the left fragment, the image has already loaded in an earlier thread. Now, with the same URL link, I am asking Picasso to place that image in the fragment on the right.
Here is the code which generates the grid view on the left fragment (and occurs first):
https://github.com/esend7881/udacity-android-popmovie/blob/a9a1b9a19a37594bb5edd736b7ec59229fb5905a/app/src/main/java/com/ericsender/android_nanodegree/popmovie/adapters/GridViewAdapter.java#L71
String load = String.format(sImgUrl, sImgSize, movie.poster_path);
Picasso.with(mContext.getApplicationContext())
.load(load)
.placeholder(R.drawable.abc_btn_rating_star_on_mtrl_alpha)
.error(R.drawable.abc_btn_rating_star_off_mtrl_alpha)
.resize(550, 775)
.into(viewHolder.imageView);
And then here is the code which runs in the right fragment:
https://github.com/esend7881/udacity-android-popmovie/blob/a9a1b9a19a37594bb5edd736b7ec59229fb5905a/app/src/main/java/com/ericsender/android_nanodegree/popmovie/fragments/MovieDetailsFragment.java#L308
Picasso.with(getActivity().getApplicationContext())
.load(String.format(sImgUrl, sImgSize, mMovieObj.poster_path))
.error(R.drawable.blank)
.fit()// .resize(366, 516)
.into(mMovieThumb, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Utils.log(sw.toString());
Utils.hideViewSafe(mMovieThumbProgress);
}
#Override
public void onError() {
Utils.log(sw.toString());
Utils.hideViewSafe(mMovieThumbProgress);
}
});
I am using the same application context in each as well as the load text:
String.format(sImgUrl, sImgSize, mMovieObj.poster_path))
and
getActivity().getApplicationContext()
So, I would think Picasso ought to detect when the exact same URL load link appears in the same context within a short period of time from each other and Picasso would then load the exact same image back into the app.
If this is not how Picasso caching works, then how does it?
As a comment mentioned, I'd guess this is affected by the size of the image being different in both fragments.
I'd recommend using https://github.com/facebook/fresco instead of picasso. It's more efficient, especially with different sizes. You can also directly access cached files if required https://github.com/facebook/fresco/issues/80
It's probably related to the HTTP headers received when getting the image that do not allow caching, as Picasso relies on an HTTP component to do the caching.
Try uploading your image on imgur, try hardcoding that path and see if it works. If that's the case, you'll have to find a workaround on how to get the image from the movie database.

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 Picasso Reload Image from cache fail

Its been sometime I started using Picasso for Image Loading in Android. The usual scenario is , it downloads images and cache it and if later needed it retrieves from Cache.
just found this use case,
if the ImageURL ends with .(jpg,png, etc etc .....)
Ex:http://www.androidguys.com/wp-content/uploads/2014/07/android-for-wallpaper-8.png
Picasso work as expected ,
but if the imageURL's are of type
http://cloud.clubsalive.com.au/download/33/404e58cf-d890-4eaa-981e-c2f3442f9348
then its not reloading from cache , its downloading every time when the view is loaded.
Is it a normal behavior of Picasso ???
Your image is giant. Try to increase cache size. Or scale down your image

Universal Image Loader - removing single image from cache not working

I calling following code before loading an image:
String url = getUrlImageIcon();
MemoryCacheUtil.removeFromCache(url, ImageLoader.getInstance().getMemoryCache());
DiscCacheUtil.removeFromCache(url, ImageLoader.getInstance().getDiscCache());
ImageLoader.getInstance().displayImage(url, imageView, listener);
My Problem is, this is not deleting the image from cache, the image loader is still displaying the old image afterwards... The old image is not even existing on the server anymore...
How can I remove all cached files from an image correctly?
PS: I'm using the up-to-date version 1.9.1...
What #vanomart answered is perfect, just to update the answer. Currently, UIL supports,
MemoryCacheUtils.removeFromCache(imageUri, imageLoader.getMemoryCache());
DiskCacheUtils.removeFromCache(imageUri, imageLoader.getDiskCache());
So, there is better way to clear disk cache.
According to developer of this library is solution quite simple. All you need to do is to delete cached image from memory and also from disk. How to do that is shown below.
File imageFile = imageLoader.getDiscCache().get(imageUri);
if (imageFile.exists()) {
imageFile.delete();
}
MemoryCacheUtils.removeFromCache(imageUri, imageLoader.getMemoryCache());
Snippet above is from this issue.

Categories

Resources