views:

81

answers:

2

Hi,

I'm trying to cache an image obtain from a webside. However, CacheManager.getCacheFileBaseDir() always returns null. Do I need to create the cache folder in the android emulator? if yes, how can I do it? in which directory i need to create it in? Thanks in advance for helping.

+1  A: 

Don't use that. There's a documentation issue per this answer. Use Context#getCacheDir for a cache dir specific to your application. You'll have to do all the cache management yourself, however, the framework will manage the contents on low disk space and when your application is uninstalled.

Qberticus
I had tried the codes posted on "this answer" link. however, I got a java.lang.nullPointerException
Lynnooi
This line always return the nullPointerException. Do I need to add any permission in order to use the cache apart from the INTERNET permission?CacheResult cache_result = CacheManager.getCacheFile(imageUrl, new HashMap());
Lynnooi
No, what I meant by posting that is that you should not use CacheManager and the documentation for it is wrong. It is not to be for use outside of webkit. You need to use `Context#getCacheDir` if you want a simple and quick cache dir.
Qberticus
ok.. got it. Thanks a lot. Is there anywhere I could get the example on how I can do the quick cache?
Lynnooi
+1  A: 

If you're going to cache a lot of data you should use SD card for that not internal memory. So you can just create a directory on SD card and save your cache in it.

File cacheDir;
//Choose chache directory
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
  cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"MyCache");
else
  cacheDir=context.getCacheDir();
//File in cache directory
File f=new File(cacheDir, "file.txt");
Fedor
If i would like to store my cache in the sdcard, how can I do it?
Lynnooi
Is there any example for me as a reference? because i'm quite new in android.
Lynnooi
Added some code
Fedor
Thanks for the example. I will give it a try.
Lynnooi