Android comes with memory memories and third-party external storage Disk Management

Source: Internet
Author: User

/** * @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;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.