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'
Related
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);
I have a small problem, as I could convert a url from an image to a bitmap. The url string is obtained through a json that I download with Volley, and I need that bitmap to be able to give a personalized icon to a marker.
I think you can use Picasso Library here, and it will show the image with the given URL dynamically. There is much more benefits of using Picasso. Try that if it helps you.
And also there is no need to download the image to the user. It saves images cache to show the image if user returns in particular time-gap.
Volley supports loading images asynchronously to a bitmap.
Picasso supports loading images asynchronously to a bitmap.
Other image loading libraries may offer similar options, though since you already use Volley and Picasso, you may wish to stick with one of those.
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
According to universal-image-loader URL how to get the path stored in
the local picture
I've had the same problems
I haven't found any of the following image loaders expose access to the underlying file "Cache";
Universal Image Loader
Volley Image Loader
Picasso
Volley, remains possible, by skipping the ImageLoader, and querying the same queue with a regular Request with the same URL. Volley will recognise the request for the file and deliver the data (in memory), from there you can save it off to somewhere more convenient like external storage.
I believe the RoboSpice also allows access to the underlying data, but I find it's lazy loading implementation completely inadequate in general.
I want to know the best way to store images from and api into room db. I'm making a sport application where I receive data and images from an api. When I'm in online mode, the images are loaded using the urls provided by the api but when offline, images should be stored and retrieved from the database in offline mode. I want to know how to convert that url of image to an image and load it in offline mode.
PS : more than 100 images will be saved, does room db fit with that? Thank you.
store your image in cache using picasso
first add this to your gradle.
implementation 'com.squareup.picasso:picasso:2.71828'
then you can call the image from your url and store it in your cache.
Picasso.get().load(YOUR_IMAGE_URL).error(R.drawable.error_img).placeholder(R.drawable.loading_image).into(YOUR_IMAGEVIEW);
Picasso will only download the image once, and if you call the same url again it will be redirected to your cache instead of downloading it again(more or less).
note: check the profilier to see the data consumption.
To continue with L2_Paver's answer.
You can also check Glide for the same purpose. You might want to consider the pros and cons of each libraries which serves well for your purpose. This answer might give you some more insights about the comparison.
Picasso v/s Imageloader v/s Fresco vs Glide