I am using the Picasso library on my Android app to load images. I would like to add an option called "Clear images cache" on my app that would remove all the downloaded images from the cache, but obviously that would remove the downloaded images from my app only (I mean not from the other apps).
Is there a simple way to do that using Picasso? Using a native component?
Thanks!
You can clear in-memory cache in Picasso only per image:
Picasso.with(context).invalidate(imagePath);
Removing all cache is somewhat tricky and described here.
File cache is delegated to HTTP Client, so it's not possible to clear it from Picasso. For more information refer this answer.
Try this line of code given below, this removes the resource inside ImageView.
Picasso.with(context).setImageResource(0);
Related
I'm a beginner in android and i want to load some images from internet in my app. I heard about libraries such as Glide and Picasso. Can anyone please tell me which library is the best.
In your title you use the word "quickly". Theres no such thing, unless you have a cache implementation. As you pointed out those two libraries help in retrieving images from the web and displaying them into imageView, also support cache features so that images previously displayed can load faster in the near future.
Simple example of Picasso:
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);
Want to know more about cache in Picasso? Check out this answer
According to this article here both are really nice to be used , but I would suggest Picasso because it just updated to 3.0 + it is lighter than Glide when it comes to :
library size and method count .
I am new to Android so I want to create a background wallpaper app. I made the offline version already which displays images from an array using ImageAdapter.
But I want make it online so that the images will be downloaded and displayed from an online database. What would be the simple and best way to do it? An example would be preferred.
You can use Picasso library. Image loading using Picasso is very easy, you can do it like this way
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
and in their website you can get every details.
and another Library is Glide. You can use Glide too for loading image..
Just use Picasso, it's pretty simple and lightweight library with good documentation. You also can save images from network with some hacks.
http://square.github.io/picasso/
I am trying to figure out the best and the most efficient way of caching images to the disk, such that they would persist even after app is killed and re-launched in airplane mode. Consider the following use case:
Open the app and get all images and display them in their respective ImageView's
Kill the app
Put device in air plane mode
Open the app again.
I am trying to get the images to persist in an offline cache so that they can be displayed in the scenario mentioned above.
I went through documentation for picasso and glide and it wasn't exactly clear if their disk caching would work in this case.
Is there a way to do this using picasso or glide? I am trying to avoid having to write a custom implementation for storing this in SQLite etc.
Glide will do this for you by default without any extra work on your side. You can also customize what versions of the requested image to store in the cache.
One important thing you need to consider is if the URLs that you use Glide to fetch images from are available offline otherwise you will need to have some way to cache those as well so that you can initiate the Glide calls when you are offline.
You can see how I did it in this project: https://github.com/KhalafMH/popular-movies-android.git
To read about how to configure Glide caching see:
http://bumptech.github.io/glide/doc/caching.html
I'm using Glide to load images in my Android app:
Glide.with(context)
.load("http://www.example.com/whatever/icon.png")
.into(imageView);
I would like to bundle some images in my app APK file to make sure that they are always available even if the device is offline. I would still like to use Glide to benefit from features such as memory caching of resized images, loading and scaling in a background thread and also to keep the interface for displaying images consistent.
Each image that I need to load like this is specified by a URL.
If there's a local version ( the local file name is generated by using a part of the URL [ e.g. replacing "http://www.example.com/" with "file:///android_asset/bundled_images/whatever/" ] in the URL string ), I would like to use that one. If not, the image needs to be downloaded from the actual URL.
Here's an example:
I have an image at http://www.example.com/whatever/icon.png
I will bundle this image in the apk under:
/assets/bundled_images/whatever/icon.png
The rule would be as follows:
If the image file exists under local assets, use that file. If the image does not exist under local assets, use the remote file.
Whenever I request an image to be loaded into an imageview, I would like to be able to use just the full URL (http://www.example.com/whatever/icon.png), and my image loader should automatically be able to check for a local copy first.
Can I do something like this with any of Glide's functionality? Yes, I could of course to the file exists check before calling glide, but it seems like a common enough use case that Glide could/should have a feature for this. Would also be nice to do the file-exists check in the background.
You can achieve required flexibility by adding custom GlideModule in the way described here: Glide Configuration. Also you need to implement appropriate Glide's Loader which would encapsulate the logic for downloading. In your case it can be something similar to UriLoader.
Glide provides an option to load a local image if the device is offline.
This can be done using the error() method.
Glide.with(this).load(imageUrl)
.error(localImageResId)
.into(mIconView);
The code above downloads an image from imageUrl and loads it into the imageView.
In case of an error (ex device offline) it loads the local image resourse.
I am using Picasso to save a collection of pictures for offline viewing. Later I need to remove some pictures manually from the cache. I can delete all files from folder, but I don't need this. I need to delete 2 or 3 files from the cache. invalidate doesn't work for me. Can anyone help me?
You can clear in-memory cache in Picasso only per image:
Picasso.with(context).invalidate(imagePath);
Removing all cache is somewhat tricky and described here.
File cache is delegated to HTTP Client, so it's not possible to clear it from Picasso. For more information refer this answer.