Android Kotlin - picasso refresh / invalidate cached image from URL - android

This is the code I'm using to show an image:
Picasso.get().load(profileImgUrl).transform(CircleTransform()).into(profileUserImage)
Now I want to refresh it when this image changes on the server
I tried:
Picasso.get().load(profileImgUrl).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).transform(CircleTransform()).into(profileUserImage)
and also
Picasso.with(getContext()).invalidate(profileImgUrl)
As suggested in countless answers but with isn't working any more so I tried
Picasso.get().invalidate(profileImgUrl)
All of this isn't working and I don't want to use the stupid workaround with adding milliseconds or anything to the image URL but just refresh it how it suppose to be possible!

Related

Load Google Static Map with Picasso on Android

I'm developing an app with Android Studio, I'd like to create a RecyclerView where I have an ImageView and upload a static image of a determining point on the map.
Like here: https://developers.google.com/maps/documentation/maps-static/intro
I tried to use this method with Picasso (Library that takes an image of a url and loads an ImageView) but nothing happens, with other urls of images on the internet that I used to test it worked and loaded in ImageView but with that nothing appears:
imageMap = findViewById(R.id.imageMap);
String lat = "-12.958811";
String lon = "-38.401606";
String url ="https://maps.googleapis.com/maps/api/staticmap?";
url+="&zoom=14";
url+="&size=330x130";
url+="&maptype=roadmap";
url+="&markers=color:green%7Clabel:G%7C"+lat+", "+lon;
url+="&key=MY_KEY";
Picasso.get().load(url).into(imageMap);
Does anyone know how to do this? With this or another method?
I have tried the methods in other posts reporting this problem but my url is correct in relation to the others.
Make sure you're using the right API key (use the one specifically for Android) and get a more detailed stacktrace by adding a simple success/error callback: Android: Picasso load image failed . how to show error message
You need to enable the billing option in your project. Google change the payment methods and now you need to enable this option in order to use some APIs.

Cache pictures with Picasso and OKHttp

I am trying to cache picture using picasso after doing some home work I figured out picasso doesn't direct cache images. Hence using help from https://gist.github.com/fada21/10655652
This is sucessfully caching image to folders. I can see the files but they don't reload when the phone is offline.
I am loading the images this way:
PicassoBigCache.INSTANCE.getPicassoBigCache(getContext().getApplicationContext()).load(pd.getImage()).placeholder(R.drawable.defaultloading).error(R.drawable.none).resize(238, 250).into(holder.image);
I fixed the loading from cache issue like this:
I used https://gist.github.com/fada21/10655652
And then did the following:
added a variable to check if internet is exists or not...
if (isnetworkavailable.equalsIgnoreCase(mContext.getResources().getString(R.string.yesnetwork)))
{
PicassoBigCache.INSTANCE.getPicassoBigCache(getContext().getApplicationContext()).load(pd.getImage()).placeholder(R.drawable.defaultloading).error(R.drawable.none).resize(238, 250).into(holder.image);
}
else
{
PicassoBigCache.INSTANCE.getPicassoBigCache(getContext().getApplicationContext()).load(pd.getImage()).placeholder(R.drawable.defaultloading).error(R.drawable.none).resize(238, 250).networkPolicy(NetworkPolicy.OFFLINE).into(holder.image);
}
This works fine if I close the app with system.exit or force close.
Not sure if this is right way but I am getting the results. Things are working smoothly. Hope this is useful for somebody!

Fresco not updating disk cache with images from the network

I have been trying to use Fresco library by Facebook.
From the documentation I understood that the caching of images works out of the box with the ImagePipelines when we use the SimpleDraweeView. I pretty much see things working perfectly only with the minor hiccup that eventhough Fresco fetches images from my URI the images in the DiskCache and other Caches are not replaced by it.
I'm positive about my code fetching the images because my images do show up the first time I run the app after I clear the cache and I implemented a RequestListener and a ControllerListener which both turned up success.
Here is my current setup:
// Setting the collaborator image.
GenericDraweeHierarchy collaboratorPicHierarchy =
genericDraweeHierarchyBuilder
.setPlaceholderImage(
getInitialsAsDrawable(
collaborator.getUser().getInitials()
)
)
.setRoundingParams(new RoundingParams()
.setRoundAsCircle(true)
)
.build();
holder.collaborator.setHierarchy(collaboratorPicHierarchy);
holder.collaborator.setImageURI(
Uri.parse(AltEngine.formURL("user/getProfilePic/") +
accessToken + "/" +
collaborator.getUser().getUuid()
)
);
All this code lies inside an Adapter and in the Adapter's constructor I have initialized Fresco.
imagePipelineConfig = ImagePipelineConfig.newBuilder(this.context)
.build();
Fresco.initialize(this.context, imagePipelineConfig);
After a lot of searching in StackOverflow I found that the following Snippet could be used.
Fresco.getImagePipelineFactory().getMainDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
Fresco.getImagePipelineFactory().getSmallImageDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
I tried that in the onRequestSuccess of ImageRequest instance which I associated with the SimpleDraweeView but that resulted in the placeholder being shown everytime I refresh the ListView which is bad.
The next solution I found was from here Android - How to get image file from Fresco disk cache? which suggested that it might be necessary to implement my own DiskCacheConfig for Fresco to invalidate my DiskCache so I tried configured my DiskCache with the code I found in the SO question.
DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder().setBaseDirectoryPath(this.context.getCacheDir())
.setBaseDirectoryName("v1")
.setMaxCacheSize(100 * ByteConstants.MB)
.setMaxCacheSizeOnLowDiskSpace(10 * ByteConstants.MB)
.setMaxCacheSizeOnVeryLowDiskSpace(5 * ByteConstants.MB)
.setVersion(1)
.build();
imagePipelineConfig = ImagePipelineConfig.newBuilder(this.context)
.setMainDiskCacheConfig(diskCacheConfig)
.build();
Fresco.initialize(this.context, imagePipelineConfig);
Still the Cache is not updated by the image from the network.
I dont know what has gone wrong here. May be I've got Fresco wrong altogether. Any how I am stuck here and dont have a clue on how to proceed. I have been through the docs a couple of times and due to my hard luck I might have missed something important. Please point me in a proper direction.
This is basically the same question as was asked here: https://github.com/facebook/fresco/issues/124.
And in fact, you had about the right idea in the two lines you pasted above with the removal from disk cache. You just need to call them in the right place. onRequestSuccess is not the right place since that will nuke the image you just downloaded.
You want to remove the old entry from cache before even sending out a request for a new one. If Fresco finds the image on disk, it won't even send out a network request.

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

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