图片的三级缓存

来源:互联网 发布:颜色识别软件下载 编辑:程序博客网 时间:2024/05/08 21:13
//SD卡的写和读的权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

package com.example.administrator.app1.utils;import android.graphics.Bitmap;import android.support.v4.util.LruCache;import android.util.Log;/** * Created by Administrator on 2016/6/13 0013. * * 内存缓存 */public class MemoryCache {    //内存管理器    private LruCache<String,Bitmap>lruCache;//类似于Map集合    public MemoryCache(){//构造方法        int maxSize;        //获得本机(可用)内存的大小        long maxMemory=Runtime.getRuntime().maxMemory();        //分配空间给该应用作为内存(八分之一)        maxSize=(int)(maxMemory/8);        Log.i("doInBackground","!!!!!!!!!本机的内存空间为:"+maxMemory);        Log.i("doInBackground","!!!!!!!!!!本机的可用内存空间为:"+maxSize);        lruCache=new LruCache<String,Bitmap>(maxSize){            @Override//该方法返回一张图片的字节总数            protected int sizeOf(String key, Bitmap value) {                return super.sizeOf(key, value);            }        };    }    //将图片放到内存中的方法    public void putImage(String path,Bitmap bitmap){        lruCache.put(path,bitmap);    }    //从内容中取出图片    public Bitmap getImage(String path){        return lruCache.get(path);    }}
</pre><pre name="code" class="java">
<pre name="code" class="java">package com.example.administrator.app1.utils;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Environment;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;/** * Created by Administrator on 2016/6/13 0013. * *  本地(SD卡)缓存 */public class LocalCache {    public static final String derictory= Environment.getExternalStorageDirectory()            .getAbsolutePath()+ File.separator+"cbk";    //从本地(SD卡)取出指定的图片    public Bitmap getBitmap(String imageName){        Bitmap bitmap=null;        File file=new File(imageName);        if(file.exists()){            try {                bitmap= BitmapFactory.decodeStream(new FileInputStream(file));            } catch (FileNotFoundException e) {                e.printStackTrace();            }        }        return bitmap;    }    //将图片放到SD卡中    public void setBitmap(String imageName,Bitmap bitmap){        File file=new File(derictory,imageName);        //判断父目录是否存在        File parentFile=file.getParentFile();        if(!parentFile.exists()){//如果目录不存在            parentFile.mkdirs();//创建目录        }        try {//把图片写入SD卡中            bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file));        } catch (FileNotFoundException e) {            e.printStackTrace();        }    }}

package com.example.administrator.app1.utils;import android.graphics.Bitmap;import android.os.AsyncTask;import android.widget.ImageView;/** * Created by Administrator on 2016/6/13 0013. * 网络请求 */public class NetCache {    private LocalCache localCache;//本地SD    private MemoryCache memoryCache;//内存    //提供构造方法    public NetCache(LocalCache localCache, MemoryCache memoryCache) {        this.localCache = localCache;        this.memoryCache = memoryCache;    }    //发起网络请求的方法    public void downImage(String path,ImageView imageView){        new ImageAysncTask(imageView).execute(path);//创建异步任务    }    //异步任务类    class ImageAysncTask extends AsyncTask<String,Void,Bitmap>{        private ImageView imageView;        private String path;        public ImageAysncTask(ImageView imageView) {            this.imageView=imageView;        }        @Override        protected Bitmap doInBackground(String... params) {            path=params[0];//下载网络图片            Bitmap bitmap=MyHttp.downImageFromNet(path,imageView);            return bitmap;        }        @Override        protected void onPostExecute(Bitmap bitmap) {            if(bitmap!=null){                //判断ImageView是否重用,解决错位的问题                if(imageView.getTag().equals(path)){                    imageView.setImageBitmap(bitmap);//给图片控件设置显示的图片                }                //把下载的图片保存到SD卡中                String filename=null;                filename=path.substring(path.lastIndexOf("/")+1);                                localCache.setBitmap(filename,bitmap);                //把图片保存到内存中                memoryCache.putImage(path,bitmap);            }        }    }}


package com.example.administrator.app1.utils;import android.graphics.Bitmap;import android.widget.ImageView;/** * Created by Administrator on 2016/6/13 0013. * * 图片缓存的管理类 */public class BitmapCache {    private LocalCache localCache;//SD卡    private MemoryCache memoryCache;//内存    private NetCache netCache;    public static BitmapCache bitmapCache=new BitmapCache();    public static BitmapCache getInstance(){        return bitmapCache;//提供方法,返回该对象(单利模式)    }    private BitmapCache(){//初始化内存,SD卡和网络请求        localCache=new LocalCache();        memoryCache=new MemoryCache();        netCache=new NetCache(localCache,memoryCache);    }    //三级缓存的方法    public void setImageViewFromCache(String path,ImageView imageView){        //1、先从内存中取图片        Bitmap bitmap=memoryCache.getImage(path);        if(bitmap!=null){//如果缓存中有该图片            imageView.setImageBitmap(bitmap);            return;        }        //2、如果内存中没有就到SD卡中找找        String filename=null;        //获得文件名        filename=path.substring(path.lastIndexOf("/")+1);        bitmap=localCache.getBitmap(filename);        if(bitmap!=null){            //获得压缩的图片            memoryCache.putImage(path,bitmap);//保存到内存中            imageView.setImageBitmap(bitmap);//设置为图片控件显示的图片            return;        }        //3、如果到这里来,就需要去网络下载图片了        netCache.downImage(path,imageView);    }}


1 0
原创粉丝点击