here the scenario
1: Open android app with glide, then glide download the image and save it in cache.
2.if app is close then reopen, then the URL is same, glide load the image from cache
3:While not connecting to internet,when open the app, i want to make glide display random image from cache, it will be nice if glide can list all the URL from cache
how to make step 3 work?
You can use DiskCacheStrategy.
Set of available caching strategies for media.
static DiskCacheStrategy ALL
Glide uses memory and disk caching by default to avoid unnecessary
network requests.
Glide.with(contextOBJ)
.load("IMAGE_LINK")
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageViewOBJ);
Related
I am developing an Android application where I need to show some images in a recyclerview. I want to download the images at once and probably save it in cache or internal storage of the android and then show the images from the internal storage if there are images in storage and if not then load and download from server.
And I don't want these images to appear in the user's Gallery App.
I just want to know the architecture , not the code about how to do it.
Is there any difference between the cache memory and internal storage? And do I need the permission to save the images in cache?
EDIT:
My question is different from the one suggested by #Dima, in that question he is not caching the images or save it in internal storage, i reckon.
Glide is a great library for showing image asynchronously.
Glide's disk cache strategies:
Glide 3.x & 4.x: DiskCacheStrategy.NONE caches nothing, as discussed
Glide 4.x: DiskCacheStrategy.DATA, Glide 3.x: DiskCacheStrategy.SOURCE caches only the original full-resolution image. In our example above that would be the 1000x1000 pixel one
Glide 4.x: DiskCacheStrategy.RESOURCE Glide 3.x: DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations) (default behavior of Glide 3.x)
Glide 4.x only: DiskCacheStrategy.AUTOMATIC intelligently chooses a cache strategy based on the resource (default behavior of Glide 4.x)
Glide 3.x & 4.x: DiskCacheStrategy.ALL caches all versions of the image
As a last example, if you've an image which you know you'll manipulate often and make a bunch of different versions of it, it makes sense to only cache the original resolution. Thus, we'd tell Glide to only keep the original:
example:
Glide 4.x
GlideApp
.with(context)
.load(eatFoodyImages[2])
.diskCacheStrategy(DiskCacheStrategy.DATA)
.into(imageView3);
Glide 3.x
Glide
.with( context )
.load( eatFoodyImages[2] )
.diskCacheStrategy( DiskCacheStrategy.SOURCE )
.into( imageViewFile );
and the cached will be in data folder of your app so won't show up in user gallery.
I'm trying to
Pre-load all my images for a grid/list into a disk cache to use later, so that the images can be displayed under poor network conditions. (Think of it like an offline photo gallery)
The cache would only be flushed on demand, never automatically.
I wish to avoid rolling my own code, and want to leverage Volley to the extent possible.
Is this possible with 100% volley? If not, can it be achieved through any other library?
I have not personally used Volley. So, I'm not sure if image caching mechanism is provided by Volley out of the box. Rather, image-loading specific libraries like Picasso provides very elegant pre-caching mechanism for you to use. If you know the list of image URLs upfront, just call the fetch method to start caching files.
// Returns Random Images always. Use your own source instead.
String[] urls = [
"http://lorempixel.com/400/200",
"http://lorempixel.com/600/400",
"http://lorempixel.com/800/600",
"http://lorempixel.com/300/150",
];
// Prefetch all images
for(String url : urls) {
Picasso
.with(getApplicationContext())
.load(url)
.fetch();
}
Once Picasso fetches all the images, it will be ready for a load() call from Picasso. Cached images will work just fine even if there is no internet connection.
Picasso
.with(getApplicationContext())
.load("http://lorempixel.com/400/200") // Already cached image
.into(yourImageView);
To add Picasso as a dependency of your project, add the following line to gradle.
compile 'com.squareup.picasso:picasso:2.5.2'
I'm using Picasso to initially download images of products to my app from a server. Since the images aren't changing nearly at all for a certain product, I save the images to the disk from the ImageView. In the app I have to display these images all over again in different sizes. To do so I'm using Picasso again, loading the saved file into an ImageView and do fit().centercrop() so I don't get any OutofMemory issues.
Since Picasso is also capable of using OkHttp's cache and doing the caching by its own, I want to know:
Are there any advantages about letting Picasso do the caching, over saving it to storage manually and handing it over again to Picasso later?
And would it be a good way to store the shrinked image (after using .fit()) as a new file so the calculation hasn't to be done all the time.
I implemented a routine which saves data from a json into database and after that if user gets offline, he can see all data. but picasso doesn't load the images after the first run. but when i run the application twice in online mode, after that picasso can load the images from cache in offline mode.
(it should cache images on the first run but it's not working)
appreciate any suggestion
https://stackoverflow.com/a/23281195/3664628
Picasso doesn't have a disk cache. It delegates to whatever HTTP client you are using for that functionality (relying on HTTP cache semantics for cache control). Because of this, the behavior you seek comes for free...
The main reason may be other images are evicting the older ones from the cache due to their size. You can load smaller versions or increase the size of the memory cache like this
Picasso p = new Picasso.Builder(context)
.memoryCache(new LruCache(Size))
.build();
If you don't want to save in cache, You can Additionally exclude that too using Memory Policy.
Picasso attempts to get the requested image from the memory first. If you would like Picasso to skip this step, you can call memoryPolicy(MemoryPolicy policy, MemoryPolicy... additional) on your Picasso request creator. MemoryPolicy is a simple enum with two values: NO_CACHE and NO_STORE. like this
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[1])
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageViewFromDisk);
Additional Source : futurestud.io
Here's what I currently understand:
I load http://www.example.com/imageA.png into my imageView using Picasso.
Picasso downloads imageA.png and in simple terms, caches it internally as "http://www.example.com/imageA.png".
I go offline, open my app, and Picasso tries to load http://www.example.com/imageA.png into my imageView again. It loads the image from the cache. I can see imageA even when my network isn't working.
This is all fine and dandy.
Say for example http://www.example.com/imageA.png now redirects to http://www.example.com/imageB.png and I repeat steps 1 and 2.
This time, I believe, Picasso caches it as http://www.example.com/imageB.png and therefore when I open my app in offline mode, I don't see imageA because it's cached as imageB.
Am I right?
Is there a way to force Picasso to cache it as its original URL?
Apparently you can add a stableKey to the request with the Request.Builder()
https://square.github.io/picasso/javadoc/com/squareup/picasso/Request.Builder.html#stableKey-java.lang.String-
Picasso manages memory cache only, so stableKey is only about that. Disk cache is managed by OkHttp. It's behaviour cannot be changed because com.squareup.okhttp.Cache is a final class.