• 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,看需求自行设置即可!

  • 相关阅读:
    beforeRouteLeave 实现vue路由拦截浏览器的需求,进行一系列操作 草稿保存等等
    VUE 路由变化页面数据不刷新问题
    vue 监听 watch 使用
    vue-cli配置文件详解
    vue-cli脚手架中webpack配置基础文件详解
    Dbus组成和原理
    NUMA架构的优缺点
    define 的全部使用方法
    敏捷(Agile)——“说三道四”
    Linux 内核模块编译 Makefile
  • 原文地址:https://www.cnblogs.com/poe-blog/p/3144516.html
Copyright © 2020-2023  润新知