图片三级缓存↙↙↙↙↙

2018-05-04  本文已影响0人  卓而不群_0137

public class MemonryCacleUtiles {


private final LruCachememorycacle;


public MemonryCacleUtiles() {


//应用程序使用的最大内存


        int maxmemopry = (int) Runtime.getRuntime().maxMemory();


//缓存的大小


        int cacsesize = maxmemopry /8;


//默认返回的图片数量


        memorycacle =new LruCache(cacsesize){


@Override


            protected int sizeOf(String key, Bitmap value) {


return value.getByteCount();


}


};


}


//图片保存到缓存


    public void saveBitmapToMemoryCache (String path,Bitmap bitmap){


memorycacle.put(path,bitmap);


}


//从缓存中获取图片


    public Bitmap getBitmapToMemoryCacle (String path) {


return memorycacle.get(path);


}


}


public class DiskCacleUtiles {


private Stringpath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/NewPhto";


//保存到SD卡中


    public void saveBitmapToDiskCache (String pathurl,Bitmap bitmap)throws UnsupportedEncodingException {


//对图片进行MD5加密


        String filename = MD5Utils.getEncryption(pathurl);


//创建一个文件


        File file =new File(path,filename);


//将图片保存到文件中


        try {


bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file));


}catch (Exception e) {


e.printStackTrace();


}


}


//获取磁盘中的图片


    public Bitmap getBitmapCacle (String pathurl)throws UnsupportedEncodingException {


String filername = MD5Utils.getEncryption(pathurl);


File file =new File(path,filername);


Bitmap bitmap =null;


try {


BitmapFactory.decodeStream(new FileInputStream(file));


}catch (FileNotFoundException e) {


try {


bitmap = BitmapFactory.decodeStream(new FileInputStream(file));


}catch (FileNotFoundException e1) {


}


}


return bitmap;


}


}


MD5加密


public static String getEncryption(String originString)


throws UnsupportedEncodingException {


String result ="";


if (originString !=null) {


try {


// 指定加密的方式为MD5


            MessageDigest md = MessageDigest.getInstance("MD5");


// 进行加密运算


            byte bytes[] = md.digest(originString.getBytes("ISO8859-1"));


for (int i =0; i < bytes.length; i++) {


// 将整数转换成十六进制形式的字符串 这里与0xff进行与运算的原因是保证转换结果为32位


                String str = Integer.toHexString(bytes[i] &0xFF);


if (str.length() ==1) {


str +="F";


}


result += str;


}


}catch (NoSuchAlgorithmException e) {


e.printStackTrace();


}


}


return result;


}


网络


private DiskCacleUtilesdiskCacleUtiles;


private MemonryCacleUtilesmemonryCacleUtiles;


public NewsWork(DiskCacleUtiles diskCacleUtiles, MemonryCacleUtiles memonryCacleUtiles) {


this.diskCacleUtiles = diskCacleUtiles;


this.memonryCacleUtiles = memonryCacleUtiles;


}


public void exeute (String path, ImageView imageView) {


BitMapTask bitMapTask =new BitMapTask();


bitMapTask.execute(path,imageView);


}


class BitMapTaskextends AsyncTask{


private Stringurlpath;


private ImageViewimageView;


private Bitmapbitmap;


@Override


    protected Bitmap doInBackground(Object... objects) {


urlpath = (String) objects[0];


imageView = ((ImageView) objects[1]);


try {


bitmap = downloadbitmap();


}catch (Exception e) {


}


return bitmap;


}


@Override


    protected void onPostExecute(Bitmap bitmap) {


if (bitmap !=null) {


//设置图片


            imageView.setImageBitmap(bitmap);


memonryCacleUtiles.saveBitmapToMemoryCache(urlpath,bitmap);


try {


diskCacleUtiles.saveBitmapToDiskCache(urlpath,bitmap);


}catch (UnsupportedEncodingException e) {


e.printStackTrace();


}


}


super.onPostExecute(bitmap);


}


private Bitmap downloadbitmap()throws Exception {


URL url =new URL(urlpath);


HttpURLConnection connection = (HttpURLConnection) url.openConnection();


if (connection.getResponseCode() ==200) {


InputStream inputStream = connection.getInputStream();


BitmapFactory.Options options =new BitmapFactory.Options();


//获取原图的大小


            options.inJustDecodeBounds=true;


//设置压缩的比例


            options.inSampleSize=2;


//再次解析图片设置为false


            options.inJustDecodeBounds=false;


Bitmap bitmap = BitmapFactory.decodeStream(inputStream,null,options);


return  bitmap;


}


return  null;


}


}



public class BitMapUtils {


private MemonryCacleUtilesmemonryCacleUtiles;


private DiskCacleUtilesdiskCacleUtiles;


private NewsWorknewsWork;


public BitMapUtils() {


this.memonryCacleUtiles =new MemonryCacleUtiles();


this.diskCacleUtiles =new DiskCacleUtiles();


this.newsWork =new NewsWork(diskCacleUtiles,memonryCacleUtiles);


}


public void showBitmap(String path, ImageView imageView) {


Bitmap bitmap =memonryCacleUtiles.getBitmapToMemoryCacle(path);


if (bitmap !=null) {


imageView.setImageBitmap(bitmap);


return;


}


try {


bitmap =diskCacleUtiles.getBitmapCacle(path);


}catch (UnsupportedEncodingException e) {


e.printStackTrace();


}


if (bitmap !=null) {


imageView.setImageBitmap(bitmap);


return;


}


newsWork.exeute(path,imageView);


}


上一篇 下一篇

猜你喜欢

热点阅读