开发者

My cache folder in Android is null. Do I have to create it?

开发者 https://www.devze.com 2023-01-04 07:56 出处:网络
I\'m trying to cache an image obtained from a website. However, CacheManager.getCacheFileBaseDir() always returns null.

I'm trying to cache an image obtained from a website. However, CacheManager.getCacheFileBaseDir() always returns null.

Do I need to create the cache folder in the android em开发者_如何学JAVAulator? If yes, how I can do that? In which directory it should be created?


Don't use that. There's a documentation issue per this answer: How to use Android's CacheManager?. 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.


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");


There is a recommendation to make use of Context.getExternalCacheDir().

This location will be exclusively available only for your application and this API will delete this location when you uninstall the application from your android device. In order to make use of this, you must make the following statement as an entry in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消