In certain situations it is very useful to look only in the local cache, such as rendering widgets where the time constraints for returning a view make it impractical to wait for the HTTP call to complete.
Something like this diff below seems like it ought to work. If it looks okay, I can make a proper PR out of it with unit tests and such.
diff --git a/picasso/src/main/java/com/squareup/picasso/Picasso.java b/picasso/src/main/java/com/squareup/picasso/Picasso.java
index a4093c1..49a2d6d 100644
--- a/picasso/src/main/java/com/squareup/picasso/Picasso.java
+++ b/picasso/src/main/java/com/squareup/picasso/Picasso.java
@@ -349,7 +349,7 @@ public class Picasso {
} else {
Response response = null;
try {
- response = loader.load(uri, request.retryCount == 0);
+ response = loader.load(uri, request.retryCount == 0 || request.cacheOnly);
if (response == null) {
return null;
}
diff --git a/picasso/src/main/java/com/squareup/picasso/Request.java b/picasso/src/main/java/com/squareup/picasso/Request.java
index ff89870..a11cc21 100644
--- a/picasso/src/main/java/com/squareup/picasso/Request.java
+++ b/picasso/src/main/java/com/squareup/picasso/Request.java
@@ -34,6 +34,7 @@ class Request implements Runnable {
final PicassoBitmapOptions options;
final List<Transformation> transformations;
final boolean skipCache;
+ final boolean cacheOnly;
final boolean noFade;
final int errorResId;
final Drawable errorDrawable;
@@ -46,7 +47,7 @@ class Request implements Runnable {
boolean retryCancelled;
Request(Picasso picasso, Uri uri, int resourceId, ImageView imageView,
- PicassoBitmapOptions options, List<Transformation> transformations, boolean skipCache,
+ PicassoBitmapOptions options, List<Transformation> transformations, boolean skipCache, boolean cacheOnly,
boolean noFade, int errorResId, Drawable errorDrawable) {
this.picasso = picasso;
this.uri = uri;
@@ -55,6 +56,7 @@ class Request implements Runnable {
this.options = options;
this.transformations = transformations;
this.skipCache = skipCache;
+ this.cacheOnly = cacheOnly;
this.noFade = noFade;
this.errorResId = errorResId;
this.errorDrawable = errorDrawable;
diff --git a/picasso/src/main/java/com/squareup/picasso/RequestBuilder.java b/picasso/src/main/java/com/squareup/picasso/RequestBuilder.java
index 74562c1..541f9c7 100644
--- a/picasso/src/main/java/com/squareup/picasso/RequestBuilder.java
+++ b/picasso/src/main/java/com/squareup/picasso/RequestBuilder.java
@@ -24,6 +24,7 @@ public class RequestBuilder {
PicassoBitmapOptions options;
private List<Transformation> transformations;
private boolean skipCache;
+ private boolean cacheOnly;
private boolean noFade;
private boolean hasNullPlaceholder;
private int placeholderResId;
@@ -123,6 +124,16 @@ public class RequestBuilder {
return this;
}
+ public RequestBuilder cacheOnly() {
+ if (this.skipCache) {
+ throw new IllegalStateException("Cannot specify cache only with skip cache.");
+ }
+
+ this.cacheOnly = true;
+
+ return this;
+ }
+
/** Resize the image to the specified dimension size. */
public RequestBuilder resizeDimen(int targetWidthResId, int targetHeightResId) {
Resources resources = picasso.context.getResources();
@@ -277,7 +288,7 @@ public class RequestBuilder {
public Bitmap get() throws IOException {
checkNotMain();
Request request =
- new Request(picasso, uri, resourceId, null, options, transformations, skipCache, false, 0,
+ new Request(picasso, uri, resourceId, null, options, transformations, skipCache, cacheOnly, false, 0,
null);
return picasso.resolveRequest(request);
}
@@ -328,7 +339,7 @@ public class RequestBuilder {
}
Request request =
- new Request(picasso, uri, resourceId, target, options, transformations, skipCache, noFade,
+ new Request(picasso, uri, resourceId, target, options, transformations, skipCache, cacheOnly, noFade,
errorResId, errorDrawable);
picasso.submit(request);
}