/** * @author [email protected] * @time 20140606 * * Package Com.intbird.utils;import Java.io.file;import Com.yilak E.store.filehelper;import Android.graphics.bitmap;import Android.os.environment;import Android.util.LruCache; public class CacheManager {//singleton private static CacheManager cacheinstance;//memory cache size private final int memo_cache_size= ( int) (Runtime.getruntime (). MaxMemory ()/1024));//File cache size private final int disk_cache_size=1024*1024*100;// Memory Cache private lrucache<string,bitmap> mmemorycache;//file cache private Disklrucache mdisklrucache;private String Cacheurl= "";p ublic CacheManager () {//Memory cache mmemorycache=new lrucache<string, bitmap> (memo_cache_size) { protected int sizeOf (String key, Bitmap Bitmap) {return bitmap.getbytecount ()/1024;};};/ /File Cache Setbitmapfilecachedir (Filehelper.disk_cache_filedir);} /** * Get a singleton * @return */public static CacheManager getinstance () {if (cacheinstance==null) {cacheinstance=new CacheManager ( );} return cacheinstance;} /** * Set cache directory; * @param cachedir */public void Setbitmapfilecachedir (String cachedir) {File cachefile=getdiskfilecache (cachedir); cacheurl= Cachefile.getabsolutepath (); Mdisklrucache=disklrucache.opencache (Cachefile, disk_cache_size);} /** * Returns the cache directory path * @return */public String Getbitmapfilecachedir () {return cacheurl;} Generic external call public void Addbitmaptocache (String fileUrl, Bitmap Bitmap) {addbitmaptomemory (FILEURL,BITMAP); Addbitmaptodisk (FILEURL,BITMAP);} Public Bitmap Getbitmapfromcache (String key) {Bitmap bmp=null;bmp=getbitmapfrommemory (key); if (bmp==null) {bmp= Getbitmapfromdisk (key);} return bmp;} /** * Picture added memory cache * @param key * @param bitmap */private void addbitmaptomemory (String key,bitmap bitmap) {if (Getbitmapfrommem Ory (key) ==null) {mmemorycache.put (key, bitmap);}} /** * Get memory Cache picture * @param key * @return */private Bitmap getbitmapfrommemory (String key) {return mmemorycache.get (key);} /** * Add picture to file cache * @param key * @param bitmap */private void Addbitmaptodisk (String key,bitmap bitmap) {if (MDISKLRU Cache! = null && mdisklrucache.Get (key) = = null) {Mdisklrucache.put (key, bitmap); }}/** * Get picture from File cache * @param key * @return */private Bitmap getbitmapfromdisk (String key) {if (Mdisklrucache! = null ) {return mdisklrucache.get (key); } return null; /** * Get Cache directory * @return */public static File Getdiskfilecache (String cachedir) {string CachePath =environment.media_mounted . Equals (Environment.getexternalstoragestate ()) | | ! Environment.isexternalstorageremovable ()? Environment.getexternalstoragedirectory (). GetPath (): Environment.getdatadirectory (). GetPath (); File file =new file (CachePath + file.separator + cachedir); if (!file.exists ()) File.mkdir (); return file;}}