Can I download images with Picasso before displaying them? I want to cache images first.
Sample scenario: User clicks on the button, sees the progressbar, and when the images have finished loading user see the images on the screen.
I tried to load images with the "get" method but that did not cache the images.
Thread thread = new Thread()
{
@Override
public void run() {
try {
Picasso picasso = PicassoOwnCache.with(getApplicationContext());
RequestCreator picassoRequest;
for (String imgUrl : imagesUrls) {
picassoRequest = picasso.load(imgUrl);
picassoRequest.get();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
This is my Picasso singleton class
public class PicassoOwnCache {
static Picasso singleton = null;
static Cache cache = null;
public static Picasso with(int cacheSize, Context context) {
if (singleton == null) {
int maxSize = calculateMemoryCacheSize(context);
cache = new LruCache(cacheSize <= maxSize ? cacheSize : maxSize);
singleton = new Picasso.Builder(context)
.memoryCache(cache)
.build();
}
return singleton;
}
public static Picasso with(Context context) {
if (singleton == null) {
cache = new LruCache(calculateMemoryCacheSize(context));
singleton = new Picasso.Builder(context)
.memoryCache(cache)
.build();
}
return singleton;
}
static int calculateMemoryCacheSize(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
int memoryClass = am.getMemoryClass();
if (largeHeap && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
memoryClass = ActivityManagerHoneycomb.getLargeMemoryClass(am);
}
return 1024 * 1024 * memoryClass / 10;//7;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static class ActivityManagerHoneycomb {
static int getLargeMemoryClass(ActivityManager activityManager) {
return activityManager.getLargeMemoryClass();
}
}
}
Next show (cached) image to the user.
Picasso picasso = PicassoOwnCache.with(getApplicationContext());
picasso.setDebugging(true) ;
RequestCreator picassoRequest;
picassoRequest = picasso.load(imgUrl);
picassoRequest
.placeholder(R.drawable.loading_logo)
.error(R.drawable.no_internet)
.fit() // I tries also without fit()
.into(holder.getImageView());
Unfortunately, this does not work. Thanks for yopur suggestions!