图片的三级缓存


//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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值