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 ImagePipeline
s 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.