I am using Picasso library to get Image from an url.
My problem is when I load an Image for the first time and I exit out of my app and after I come back my app tries to load the Image again but I don't want it happen. Are there any other way to do that(load just one time the Image and in others time don't need to Internet for load)?
Asked
Active
Viewed 2,078 times
0

Kunal Yadav
- 139
- 10

abolfazl-sh
- 25
- 1
- 11
-
Please post you code for using Picasso. – Vindhya Pratap Singh Aug 24 '16 at 13:04
-
Use _Cache_ strategy. – Piyush Aug 24 '16 at 13:06
-
Picasso.with(activity).load(cv.getAsString("Picture")).placeholder(R.mipmap.onloading).error(R.mipmap.onloading).into(ivpicture); – abolfazl-sh Aug 24 '16 at 13:06
-
See this post http://stackoverflow.com/questions/23391523/load-images-from-disk-cache-with-picasso-if-offline – an_droid_dev Aug 24 '16 at 13:08
3 Answers
1
The issue with the above answers is that they only check the availability of the images in the disk cache, it does not cover the part if the image does not exist in the cache to go online and retrieve it.
First make a class that extends Application (You can name it whatever you want that does not interfere with your application, my convention is to use "Global").
public class Global extends Application {
@Override
public void onCreate() {
super.onCreate();
Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(new OkHttpDownloader(this,Integer.MAX_VALUE));
Picasso built = builder.build();
built.setIndicatorsEnabled(false);
built.setLoggingEnabled(true);
Picasso.setSingletonInstance(built);
}
}
Make sure you add dependancy for OkHttp library, it's developed by the same guys from Picasso
compile 'com.squareup.okhttp:okhttp:2.4.0'
and add the class in your Manifest file Applications tag :
android:name=".Global"
Then when you want to retrieve the image :
Picasso.with(context)
.load(Image URL)
.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(Image URL)
.into(imageView, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
Log.v("Picasso","Could not fetch image");
}
});
}
});
The above method checks if the image is already cached, if not gets it from the internet.

RamithDR
- 2,103
- 2
- 25
- 34
-
thanks.But if I don't make the Global class what happen?(in fact I don't understand what did it do) – abolfazl-sh Aug 24 '16 at 14:11
-
Uses of the application class is to access variables across the application, it's a good choice to set all these Picasso settings since the application class is started before Activities or Services are being run. – RamithDR Aug 24 '16 at 14:16
-
You can read about Application class and it's uses here : http://stackoverflow.com/questions/18002227/why-extend-an-application-class – RamithDR Aug 24 '16 at 14:22
-
0
try this:
Picasso.with(this).load(url).networkPolicy(NetworkPolicy.OFFLINE).into(imageView);

Aman Grover
- 1,621
- 1
- 21
- 41
0
try this:
Picasso.with(this).invalidate(url);
Picasso.with(this)
.load(url)
.networkPolicy(
NetworkUtils.isConnected(this) ?
NetworkPolicy.NO_CACHE : NetworkPolicy.OFFLINE)
.resize(200, 200)
.centerCrop()
.placeholder(R.mipmap.ic_avatar)
.error(R.mipmap.ic_avatar)
.into(imageView);

Shridutt Kothari
- 7,326
- 3
- 41
- 61
-
and another thing I read at another site if the Image isn't in cache this way make a problem.yes? – abolfazl-sh Aug 24 '16 at 13:20