Mitkins
Mitkins

Reputation: 4341

Picasso - Accessing memory cache only

I'm writing a gallery view and I have two sets of images; low quality and high quality.

In the gallery activity, images are shown one at a time. I'm showing the low quality image first, then the high quality image (in-place).

The low quality images have already been accessed in a previous screen and should be pre-cached in memory. What I'd like to do is access the in-memory cache only - then move onto the high quality image (regardless of whether the low quality image loaded or not).

Then it's business as usual, Picasso can download the high quality images if necessary.

Can I achieve what I want by using NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE and NetworkPolicy.OFFLINE together?

Upvotes: 2

Views: 1410

Answers (1)

Alexandre G
Alexandre G

Reputation: 1693

You probably figured it out by now, but for others landing here - yes, you certainly can.

Picasso.with(context)
    .load(smallImageUrl)
    .networkPolicy(NetworkPolicy.OFFLINE, NetworkPolicy.NO_CACHE)
    .into(imageView, new Callback() {          
        @Override
            public void onSuccess() {
            Picasso.with(context).load(largeImageUrl).into(imageView);
        }

        @Override
            public void onError() {
            Picasso.with(context).load(largeImageUrl).into(imageView);
        }
});

(excuse the non-adherence to DRY principle for brevity)

So NetworkPolicy.OFFLINE prevents it from trying to fetch off the network, and NetworkPolicy.NO_CACHE prevents checking disk cache (which is network cache from either OkHttp or HttpUrlConnection depending on what you use).

I would also recommend leaving Picasso with the latter, as it is usually perceptively just as fast as memory cache.

Upvotes: 1

Related Questions