How can I cache images after they are downloaded from web?
And now the punchline: use the system cache.
URL url = new URL(strUrl);
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Object response = connection.getContent();
if (response instanceof Bitmap) {
Bitmap bitmap = (Bitmap)response;
}
Provides both memory and flash-rom cache, shared with the browser.
grr. I wish somebody had told ME that before i wrote my own cache manager.
Regarding the elegant connection.setUseCaches solution above: sadly, it won't work without some additional effort. You will need to install a ResponseCache using ResponseCache.setDefault. Otherwise, HttpURLConnection will silently ignore the setUseCaches(true) bit.
See the comments at the top of FileResponseCache.java for details:
http://libs-for-android.googlecode.com/svn/reference/com/google/android/filecache/FileResponseCache.html
(I'd post this in a comment, but I apparently don't have enough SO karma.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With