1

I want to prefetch images using Picasso and save them all to disk upon opening the app (no lazy loading on the fly). To make sure the cache is big enough I am using the following code in my Application onCreate

ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);

OkHttpClient client = new OkHttpClient.Builder()
        .cache(new Cache(directory, Integer.MAX_VALUE))
        .build();
OkHttp3Downloader okHttp3Downloader = new OkHttp3Downloader(client);
Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(okHttp3Downloader);
Picasso built = builder.build();
built.setIndicatorsEnabled(false);
built.setLoggingEnabled(false);
Picasso.setSingletonInstance(built);

So here I set my cache size to Integer.MAX_VALUE which should be big enough ;)

I use code similar to this line to prefetch the image: Picasso.with(context).load(url).fetch();.

Now when I kill my internet and mobile data, no images are loaded even though the my code is fetching items. Any ideas why?

Gooey
  • 4,740
  • 10
  • 42
  • 76
  • in general this seems like a very bad idea to download all images on forehand. I can't imagine the amount of space you are allocating on people's precious disk space. – timr Jun 18 '16 at 14:35
  • @tim I fully agree. Wasn't my descision but lazy loading wasn't an option. – Gooey Jun 18 '16 at 14:36

3 Answers3

0

Maybe the problem is loosing state. Did you try to extend Application class (like this) and keep Picasso instance here?

In similar situation when I needed to fetch lots of images, I save them to internal memory (not just in some cache) - to prevent from reloading, and save uri's to files into SQLite and then use Picasso to work with such URI's. (don't forget to check that user don't delete anything).

Community
  • 1
  • 1
Andrey Suvorov
  • 511
  • 3
  • 17
0

In the end I created my own image manager that downloads and store images (still using picasso to download them).

Gooey
  • 4,740
  • 10
  • 42
  • 76
  • May I ask for more information on the Image Manager? What downloader are you you using? What is your process of downloading and storing the images? Are you running an `IntentService` to do the job? I am trying to do something similar with JobScheduler but the activity is being destroyed before the job is completing and job gets cancelled. – Anshul Vyas Sep 24 '18 at 17:19
0

Here is my approach:

In Application's onCreate method do:

Picasso picasso = new Picasso.Builder(this)
        .downloader(new OkHttp3Downloader(this,Integer.MAX_VALUE))
        .build();
picasso.setIndicatorsEnabled(BuildConfig.DEBUG);
picasso.setLoggingEnabled(BuildConfig.DEBUG);
Picasso.setSingletonInstance(picasso);

And then wherever you want to load your images call this for each image's url:

Picasso.with(context).load(url).fetch();

From doc:

fetch()

Asynchronously fulfills the request without a ImageView or Target. This is useful when you want to warm up the cache with an image.

You can even pass in callback to check for possible errors

Community
  • 1
  • 1
Jan Málek
  • 531
  • 8
  • 21