12

I'm currently developing an Android application that fetches images using http requests. It would be quite swell if I could cache those images in order to improve to performance and bandwidth use.

I came across the CacheManager class in the Android reference, but I don't really know how to use it, or what it really does.

I already scoped through this example, but I need some help understanding it:

/core/java/android/webkit/gears/ApacheHttpRequestAndroid.java

Also, the reference states:

"Network requests are provided to this component and if they can not be resolved by the cache, the HTTP headers are attached, as appropriate, to the request for revalidation of content."

I'm not sure what this means or how it would work for me, since CacheManager's getCacheFile accepts only a String URL and a Map containing the headers. Not sure what the attachment mentioned means.

An explanation or a simple code example would really do my day. Thanks!

Update

Here's what I have right now. I am clearly doing it wrong, just don't know where.

 public static Bitmap getRemoteImage(String imageUrl) { URL aURL = null; URLConnection conn = null; Bitmap bmp = null; CacheResult cache_result = CacheManager.getCacheFile(imageUrl, new HashMap()); if (cache_result == null) { try { aURL = new URL(imageUrl); conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); cache_result = new CacheManager.CacheResult(); copyStream(is, cache_result.getOutputStream()); CacheManager.saveCacheFile(imageUrl, cache_result); } catch (Exception e) { return null; } } bmp = BitmapFactory.decodeStream(cache_result.getInputStream()); return bmp; } 
2
  • what errors do you get with this code? Commented Mar 16, 2010 at 9:16
  • E/AndroidRuntime( 3097): FATAL EXCEPTION: main E/AndroidRuntime( 3097): java.lang.NullPointerException E/AndroidRuntime( 3097): at android.webkit.CacheManager.saveCacheFile(CacheManager.java:459) is what I get when trying to save to the cache, added a bounty if this is possible I would like to know how. E/AndroidRuntime( 3097): at android.webkit.CacheManager.saveCacheFile(CacheManager.java:453) Commented Dec 20, 2010 at 20:56

2 Answers 2

10

I don't think the CacheManger can be used outside of a WebView as noted in this bug report http://code.google.com/p/android/issues/detail?id=7222

Sign up to request clarification or add additional context in comments.

2 Comments

That's right.. I had the same issue and after two days of struggling I read that bug report :)
If you are looking for HTTP request caching on android then one option is volley: stackoverflow.com/questions/tagged/android-volley
1

I came across this issue awhile ago as well. The cache manager is only for the webview and not really useful outside of that. For my application I needed to cache xml responses and images so I ended up writing my own cache manager to accomplish that. Nothing too terrible but certainly not as easy as using a would-be built-in one.

If you have any questions about the specifics, add a comment to my post, I check back frequently.

2 Comments

Can you provide more details about the implementation?
@sabbour what do you want to know? Basically I opened a http stream and wrote it to a file. When my application accessed that file it checked the age of the file, if it was too old it deletes it so my app knows to download a new copy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.