三级缓存的原理代码实现


1.在activity中书写imageview的控件

2.调用工具类BitmapUtils;

在工具类中具体实现三级缓存的原理展示代码如下:

 Context context;
    //图片本地缓存路径
    private final static String SDCARD_CACHE = Environment.getExternalStorageDirectory() + "/imagecache";
    //图片存放文件夹
    File fileDir = new File(SDCARD_CACHE);

    private Map> map = new HashMap>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    ImageViewToBitmap imageViewToBitmap = (ImageViewToBitmap) msg.obj;
                    imageViewToBitmap.iv.setImageBitmap(imageViewToBitmap.bitmap);
                    break;
            }
        }
    };

    //构造方法
    public BitmapUtils(Context context) {
        this.context = context;
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
    }

    //加载图片的方法
    public void display(ImageView iv, String url) {
        //内存缓存

        Bitmap bitmap = loadMemory(url);
        if (bitmap != null) {
            iv.setImageBitmap(bitmap);
        } else {
            //sdcard缓存
            bitmap = loadSD(url);
            if (bitmap != null) {
                iv.setImageBitmap(bitmap);
            } else {
                //网络缓存
                loadInternetImage(iv, url);
            }
        }


    }

    private Bitmap loadMemory(String url) {
        SoftReference value = map.get(url);
        if (value != null) {
            Bitmap bitmap = value.get();
            return bitmap;
        }
        return null;

    }

    //获取本地图片
    private Bitmap loadSD(String url) {
        String name = getFileName(url);
        File file = new File(fileDir, name);
        if (file.exists()) {
            //BitmapFactory选项
            BitmapFactory.Options options = new BitmapFactory.Options();
            //加载图片宽高
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(name, options);
            //获取图片和手机屏幕宽高
            int outWidth = options.outWidth;
            int outHeight = options.outHeight;
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            int widthPixels = displayMetrics.widthPixels;
            int heightPixels = displayMetrics.heightPixels;
            //图片跟手机屏幕进行比对
            int scale = 0;
            int scaleX = outWidth / widthPixels;
            int scaleY = outHeight / heightPixels;
            scale = scaleX > scaleY ? scaleX : scaleY;
            if (scale == 0) {
                scale = 1;
            }
            options.inJustDecodeBounds = false;
            options.inSampleSize = scale;
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
            //内存缓存
            SoftReference value = new SoftReference(bitmap);
            map.put(name, value);
            return bitmap;
        }


        return null;

    }

    //获取网络图片
    private void loadInternetImage(ImageView iv, String url) {
        //开子线程做耗时操作
        new Thread(new DownloadImage(iv, url)).start();

    }

    private class DownloadImage implements Runnable {
        ImageView iv;
        String url;
        private InputStream inputStream;
        private FileOutputStream fos;

        public DownloadImage(ImageView iv, String url) {
            this.iv = iv;
            this.url = url;
        }

        @Override
        public void run() {
            try {
            HttpClient clent = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);

                HttpResponse execute = clent.execute(get);
                if (execute.getStatusLine().getStatusCode() == 200) {
                    inputStream = execute.getEntity().getContent();
                    String name = getFileName(url);
                    File file = new File(fileDir, name);
                    fos = new FileOutputStream(file);
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = inputStream.read(buffer)) != -1) {
                        fos.write(buffer, 0, len);
                    }

                    //sdcard缓存
                    Bitmap bitmap = loadSD(name);
                    //ImageView转换成Bitmap转换成Bitmap
                    ImageViewToBitmap ivtb = new ImageViewToBitmap(iv, bitmap);
                    Message message = Message.obtain(handler, 0, ivtb);
                    message.sendToTarget();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }


    }

    //获取图片的名称
    private String getFileName(String url) {
        return Md5Utils.encode(url) + ".jpg";

    }

    //ImageView转换成Bitmap转换成Bitmap
    private class ImageViewToBitmap {
        ImageView iv;
        Bitmap bitmap;

        public ImageViewToBitmap(ImageView iv, Bitmap bitmap) {
            this.iv = iv;
            this.bitmap = bitmap;
        }
    }
}

4,在工具类中的缓存中需要使用MD5,所以需要在谢一个工具类

public class Md5Utils {
    public static String encode(String password){
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] result = digest.digest(password.getBytes());
            StringBuffer sb = new StringBuffer();
            for(byte b : result){
                int number = (int)(b & 0xff) ;
                String str = Integer.toHexString(number);
                if(str.length()==1){
                    sb.append("0");
                }
                sb.append(str);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            //can't reach
            return "";
        }
    }
}



5,最后我们在activity中调用方法就能用到我们刚刚封装的工具类

private String url = "http://d.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fe871b36bad64034f79f019d4.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        ImageView iv = (ImageView) findViewById(R.id.iv);
        //实例化图片加载框架
        BitmapUtils bitmapUtils = new BitmapUtils(this);
        bitmapUtils.display(iv, url);


    }


以上就是三级缓存的原理实现

你可能感兴趣的:(三级缓存的原理代码实现)