• android内置存储器memory和第三方外部存储disk管理


    缓存管理这里

    http://blog.csdn.net/intbird/article/details/38338713

    图片处理在这里

    http://blog.csdn.net/intbird/article/details/38338811

    界面回收图片在这里

    http://blog.csdn.net/intbird/article/details/19905549

    工具Jar包在这里说明:

    http://pan.baidu.com/s/1c0q2SzY

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

    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    红队核心工具介绍
    cms漏洞总结(二)
    cms漏洞总结(二)
    cms漏洞总结(二)
    cms漏洞总结(二)
    cms漏洞总结(二)
    cms漏洞总结(二)
    cms漏洞总结 (一)
    好看的樱花落特效
    SELinux 案例 1
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4740222.html
Copyright © 2020-2023  润新知