Open GoogleCodeExporter opened 8 years ago
I have the same problem on Android 4.0.4. I have tried the Workaround above
(thanks for the post) but it didn't change the fact that the image is
redownloaded each time I need it.
I have logged the process and here is what I can see: I try to display the
already downloaded image, the [FileResponseCache].get(...) is called (I've
checked and getFile(...) returns the correct filename) and returns a
CacheResponse using createCacheResponse(file), but right after that the
[FileResponseCache].put() is called, resulting to the re-download of the image.
Any idea ? Thanks for your help
Original comment by franr...@gmail.com
on 8 Jun 2012 at 1:42
I have been using a back port component of HttpResponseCache in Android 3.0+
from https://github.com/candrews/HttpResponseCache
It is actually working well together with image-loader.
Original comment by oasisfeng
on 24 Jun 2012 at 1:46
I've found in order to get this to work I need to set some other headers as
well or the response is incorrectly identified as stale. The following code
should fix the problem (it goes in your class which inherits from
FileResponseCache)
@Override
public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException {
final CacheResponse response = super.get(uri, requestMethod, requestHeaders);
if (response == null) return null;
/* Workaround for HttpEngine to correctly identify cache source */
final Map<String, List<String>> headers = response.getHeaders();
addHeader(headers, null, "HTTP/1.1 200 OK");
addHeader(headers, "cache-control", "max-age=99999");
String now = String.valueOf(new Date().getTime());
addHeader(headers, "X-Android-Received-Millis", now);
addHeader(headers, "X-Android-Sent-Millis", now);
return response;
}
Original comment by rupert.b...@guardian.co.uk
on 11 Oct 2012 at 11:04
First, thanks a lot for the solutuion!
I noticed similarly that the cache wasn't working correctly on newer Androids,
and it was still going at the network - visible when passing the traffic
through Fiddler (Windows) or Squid (Unix, tail -f /var/log/squid3/access.log).
Debugging through the sources after reading this thread I found that the in
HttpEngine.initResponseSource() the check was always coming back with
ResponseSource.NETWORK, and was mainly because the headers didn't contain the
"null" status code entry for RawHeaders.fromMultimap() to call in
setStatusLine() and set a valid responseCode (-1 otherwise).
I finally made the change in my clone (dandar33-libs-for-android) in
FileCacheResponse.getHeaders() as it seemed a better place for it and I need it
in a couple of other projects - I tested it on Android 3.0 and earlier and the
fix doesn't seem to be needed. In fact, it will throw an NPE in 2.3 as it holds
the headers in a SortedMap. So I made the fix for 200 OK for API 14 and later.
Thanks again for the good work, this annoyed me for quite a while!
References:
http://source.android.com/source/build-numbers.html
https://android.googlesource.com/platform/libcore/+/android-4.0.4_r1/luni/src/ma
in/java/libcore/net/http/HttpEngine.java
https://android.googlesource.com/platform/libcore/+/android-4.0.4_r1/luni/src/ma
in/java/libcore/net/http/RawHeaders.java
Original comment by dan.dar33@gmail.com
on 9 Jun 2013 at 9:54
Original issue reported on code.google.com by
oasisfeng
on 4 Jun 2012 at 10:11