android对大图片的缓存处理

废话不多说,直接上代码

package com.huge.emj.common.util;



import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStream;

import java.lang.ref.SoftReference;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;



import com.huge.emj.EApplication;



import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.BitmapFactory.Options;

import android.graphics.drawable.BitmapDrawable;

import android.os.Handler;

import android.os.Message;

import android.util.Log;



public class AsyncImageLoader {



    public static HashMap<String, Bitmap> imageCache = new HashMap<String, Bitmap>();

    public static List<String> path_list = new ArrayList<String>();

    public String durl = null;

    public InputStream dinStream = null;



    public void loadDrawable(final String imageUrl,

            final ImageCallback imageCallback) {

//回调

        final Handler handler = new Handler() {

            public void handleMessage(Message message) {

                imageCallback.imageLoaded((Bitmap) message.obj,

                        imageUrl);

            }

        };

        

        synchronized (AsyncImageLoader.this) {

            // 如果已经有了这个下载的线程则不启动

            if (!path_list.contains(imageUrl)) {

                new Thread() {

                    @Override

                    public void run() {

                        Bitmap drawable = null;



                        // 先证明自己抢到了优先下载的权利

                        path_list.add(imageUrl);

                        try {

                            drawable = loadImageFromUrl(imageUrl);

                        } catch (Exception e) {



                        }

                        // remove the top from stack

                        if (path_list.size() > 3) {

                            removeImageCache();

                        }



                        // store the image url

                        if (drawable != null) {

                            imageCache.put(imageUrl,drawable);

                        }else{

                            path_list.remove(imageUrl);

                        }

                        

                        Message message = handler.obtainMessage(0, drawable);

                        handler.sendMessage(message);

                    }

                }.start();

            }else{

                if(imageCache.get(imageUrl)!=null){

                    Message message = handler.obtainMessage(0, imageCache.get(imageUrl));

                    handler.sendMessage(message);

                }

            }

        }

    }



    /**

     * 删除很久不用的数据

     */

    public void removeImageCache() {

        try{

        Bitmap bmap=imageCache.get(path_list.get(0));

        imageCache.remove(path_list.get(0));

        path_list.remove(0);

        

        if(bmap!=null){

            bmap.recycle();

        }

        }catch(Exception e){

            e.printStackTrace();

            Log.i("poe", "AsyncImageLoader 清除缓存图片失败!!!!!!!!!!============》》");

        }

    }



    /**

     * @param url    本地地址 

     * @return

     */

    private Bitmap loadImageFromUrl(final String url) {

//        BitmapDrawable d = null;

        Bitmap d = null;

        try {

            File file=new File(url);

            

            long pic_size=file.length();

            

            Options mOptions=getSampleOption(pic_size);

            

            d = BitmapFactory.decodeFile(url, mOptions);

            

//            dinStream = new FileInputStream(file);

//            d = new BitmapDrawable(dinStream);

            //判断图片的大小

        } catch (Exception e) {

             e.printStackTrace();

             LogUtil.log(EApplication.mLog, e);

        }



        return d;

    }

    

    /**

     * 单个图片语序的最大值放大到800kB

     * @param Size

     * @return

     */

    private BitmapFactory.Options getSampleOption(long Size){

        

        BitmapFactory.Options option =new Options();

        

        long fileSize=Size/1024;//kB

        

        int scaleParam = 1;

        

        //200kB以下

        if(fileSize<MAX_SINGLE_PICK_SIZE){

            scaleParam = 1;

        }else{

            scaleParam=(int) (fileSize/MAX_SINGLE_PICK_SIZE);

        }

        

        option.inSampleSize=scaleParam;

        

        

        return option;

    }



    public interface ImageCallback {

        public void imageLoaded(Bitmap imageDrawable, String imageUrl);

    }



    public static final int MAX_SINGLE_PICK_SIZE=800;

}

 

单个图片我这里是设置了最大值800kB,看需求自行设置即可!

你可能感兴趣的:(android)