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!
Related
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.
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.
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)
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.
I have trouble when I'm using Glide in my app. As I've understood, if image was downloaded once and I request image from cache from other activity, Glide must show image quick. And I got this behavior, but not in my app. Image loads very slow (about 3 seconds), although in another app it was about 0.4 second.
My code with calling Glide:
Glide.with(this)
.load(url)
.signature(new StringSignature(url))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(mHeader);
And in other activity code are same.
May you help me?
Thanks
You don't need the signature(url) part, the model (url String in your case) is already a part of the cache key.
The problem may be that your header changes size. The view size (= resulting Bitmap size) needs to be constant for a cache hit. However since you're doing ALL caching the load should still be fast. Is there anything changing in the url maybe, like a sessionid or similar? That would make the cache miss.
If the url you're loading is an animated GIF RESULT caching can be the culprit, here's a reference.