首页 > 代码库 > android自带的内存memory和第三方外部存储disk管理

android自带的内存memory和第三方外部存储disk管理

/** 
 * @author intbird@163.com 
 * @time 20140606 
 */ 
package com.intbird.utils;

import java.io.File;

import com.yilake.store.FileHelper;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.LruCache;

public class CacheManager {
	//单例
	private static CacheManager cacheInstance;
	
	//内存缓存大小
	private final int MEMO_CACHE_SIZE=((int)(Runtime.getRuntime().maxMemory()/1024));
	//文件缓存大小
	private final int DISK_CACHE_SIZE=1024*1024*100;
	//内存缓存
	private LruCache<String,Bitmap> mMemoryCache;
	//文件缓存
	private DiskLruCache mDiskLruCache;
	private String cacheUrl="";
	
	public CacheManager() {
		//内存缓存
		mMemoryCache=new LruCache<String, Bitmap>(MEMO_CACHE_SIZE){
			protected int sizeOf(String key, Bitmap bitmap) {
				return bitmap.getByteCount()/1024;
			};
		};
		//文件缓存
		setBitmapFileCacheDir(FileHelper.DISK_CACHE_FILEDIR);
	}
	
	/**
	 * 获取单例
	 * @return
	 */
	public static CacheManager getInstance() {
		if(cacheInstance==null){
			cacheInstance=new CacheManager();
		}
		return cacheInstance;
	}
	
	/**
	 * 设置缓存目录;
	 * @param cacheDir
	 */
	public void setBitmapFileCacheDir(String cacheDir){
		File cacheFile=	getDiskFileCache(cacheDir);
		cacheUrl=cacheFile.getAbsolutePath();
		mDiskLruCache=DiskLruCache.openCache(cacheFile, DISK_CACHE_SIZE);
	}
	/**
	 * 返回缓存目录路径
	 * @return
	 */
	public String getBitmapFileCacheDir(){
		return cacheUrl;
	}
	
	//通用外部调用
	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;
	}

	/**
	 * 图片加入内存缓存
	 * @param key
	 * @param bitmap
	 */
	private void addBitmapToMemory(String key,Bitmap bitmap){
		if(getBitmapFromMemory(key)==null){
			mMemoryCache.put(key, bitmap);
		}
	}
	/**
	 * 获取内存缓存图片
	 * @param key
	 * @return
	 */
	private Bitmap getBitmapFromMemory(String key){
		return mMemoryCache.get(key);
	}
	/**
	
	 * 将图片加入文件缓存
	 * @param key
	 * @param bitmap
	 */
	private void addBitmapToDisk(String key,Bitmap bitmap){
        if (mDiskLruCache != null && mDiskLruCache.get(key) == null) {
            mDiskLruCache.put(key, bitmap);
        }
	}
	/**
	 * 从文件缓存中获取图片
	 * @param key
	 * @return
	 */
	private Bitmap getBitmapFromDisk(String key){
        if (mDiskLruCache != null) {
            return mDiskLruCache.get(key);
        }
	    return null;
	}
	
	/**
	 * 获取缓存目录
	 * @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;
	}
}