转自:http://www.oschina.net/code/snippet_219356_18887?p=3#comments

ImageManager2这个类具有异步从网络下载图片,从sd读取本地图片,内存缓存,硬盘缓存,图片使用动画渐现等功能,已经将其应用在包含大量图片的应用中一年多,没有出现oom

Android程序常常会内存溢出,网上也有很多解决方案,如软引用,手动调用recycle等等。但经过我们实践发现这些方案,都没能起到很好的效果,我们的应用依然会出现很多oom,尤其我们的应用包含大量的图片。android3.0之后软引用基本已经失效,因为虚拟机只要碰到软引用就回收,所以带不来任何性能的提升。

我这里的解决方案是HandlerThread(异步加载)+LruCache(内存缓存)+DiskLruCache(硬盘缓存)。

作为程序员,我也不多说,直接和大家共享我的代码,用代码交流更方便些。

源码demo地址:https://github.com/yueyueniao2012/multiimagechooser

标签:
<无>

代码片段(1)
[全屏查看所有代码]

1. [文件]
ImageManager2.java ~ 13KB    
下载(468)    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package com.example.util;
 
import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
 
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.media.ThumbnailUtils;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v4.util.LruCache;
import android.widget.ImageView;
 
import com.example.MyApplication;
 
/**
 * 图片加载类
 *
 * @author 月月鸟
 */
public class ImageManager2 {
 
    private static ImageManager2 imageManager;
    public LruCache<String, Bitmap> mMemoryCache;
    private static final int DISK_CACHE_SIZE = 1024 * 1024 * 20; // 10MB
    private static final String DISK_CACHE_SUBDIR = "thumbnails";
    public DiskLruCache mDiskCache;
    private static MyApplication myapp;
 
    /** 图片加载队列,后进先出 */
    private Stack<ImageRef> mImageQueue = new Stack<ImageRef>();
 
    /** 图片请求队列,先进先出,用于存放已发送的请求。 */
    private Queue<ImageRef> mRequestQueue = new LinkedList<ImageRef>();
 
    /** 图片加载线程消息处理器 */
    private Handler mImageLoaderHandler;
 
    /** 图片加载线程是否就绪 */
    private boolean mImageLoaderIdle = true;
 
    /** 请求图片 */
    private static final int MSG_REQUEST = 1;
    /** 图片加载完成 */
    private static final int MSG_REPLY = 2;
    /** 中止图片加载线程 */
    private static final int MSG_STOP = 3;
    /** 如果图片是从网络加载,则应用渐显动画,如果从缓存读出则不应用动画 */
    private boolean isFromNet = true;
 
    /**
     * 获取单例,只能在UI线程中使用。
     *
     * @param context
     * @return
     */
    public static ImageManager2 from(Context context) {
 
        // 如果不在ui线程中,则抛出异常
        if (Looper.myLooper() != Looper.getMainLooper()) {
            throw new RuntimeException("Cannot instantiate outside UI thread.");
        }
 
        if (myapp == null) {
            myapp = (MyApplication) context.getApplicationContext();
        }
 
        if (imageManager == null) {
            imageManager = new ImageManager2(myapp);
        }
 
        return imageManager;
    }
 
    /**
     * 私有构造函数,保证单例模式
     *
     * @param context
     */
    private ImageManager2(Context context) {
        int memClass = ((ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
        memClass = memClass > 32 ? 32 : memClass;
        // 使用可用内存的1/8作为图片缓存
        final int cacheSize = 1024 * 1024 * memClass / 8;
 
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
 
            protected int sizeOf(String key, Bitmap bitmap) {
                return bitmap.getRowBytes() * bitmap.getHeight();
            }
 
        };
 
        File cacheDir = DiskLruCache
                .getDiskCacheDir(context, DISK_CACHE_SUBDIR);
        mDiskCache = DiskLruCache.openCache(context, cacheDir, DISK_CACHE_SIZE);
 
    }
 
    /**
     * 存放图片信息
     */
    class ImageRef {
 
        /** 图片对应ImageView控件 */
        ImageView imageView;
        /** 图片URL地址 */
        String url;
        /** 图片缓存路径 */
        String filePath;
        /** 默认图资源ID */
        int resId;
        int width = 0;
        int height = 0;
 
        /**
         * 构造函数
         *
         * @param imageView
         * @param url
         * @param resId
         * @param filePath
         */
        ImageRef(ImageView imageView, String url, String filePath, int resId) {
            this.imageView = imageView;
            this.url = url;
            this.filePath = filePath;
            this.resId = resId;
        }
 
        ImageRef(ImageView imageView, String url, String filePath, int resId,
                int width, int height) {
            this.imageView = imageView;
            this.url = url;
            this.filePath = filePath;
            this.resId = resId;
            this.width = width;
            this.height = height;
        }
 
    }
 
    /**
     * 显示图片
     *
     * @param imageView
     * @param url
     * @param resId
     */
    public void displayImage(ImageView imageView, String url, int resId) {
        if (imageView == null) {
            return;
        }
        if (imageView.getTag() != null
                && imageView.getTag().toString().equals(url)) {
            return;
        }
        if (resId >= 0) {
            if (imageView.getBackground() == null) {
                imageView.setBackgroundResource(resId);
            }
            imageView.setImageDrawable(null);
 
        }
        if (url == null || url.equals("")) {
            return;
        }
 
        // 添加url tag
        imageView.setTag(url);
 
        // 读取map缓存
        Bitmap bitmap = mMemoryCache.get(url);
        if (bitmap != null) {
            setImageBitmap(imageView, bitmap, false);
            return;
        }
 
        // 生成文件名
        String filePath = urlToFilePath(url);
        if (filePath == null) {
            return;
        }
 
        queueImage(new ImageRef(imageView, url, filePath, resId));
    }
 
    /**
     * 显示图片固定大小图片的缩略图,一般用于显示列表的图片,可以大大减小内存使用
     *
     * @param imageView 加载图片的控件
     * @param url 加载地址
     * @param resId 默认图片
     * @param width 指定宽度
     * @param height 指定高度
     */
    public void displayImage(ImageView imageView, String url, int resId,
            int width, int height) {
        if (imageView == null) {
            return;
        }
        if (resId >= 0) {
 
            if (imageView.getBackground() == null) {
                imageView.setBackgroundResource(resId);
            }
            imageView.setImageDrawable(null);
 
        }
        if (url == null || url.equals("")) {
            return;
        }
 
        // 添加url tag
        imageView.setTag(url);
        // 读取map缓存
        Bitmap bitmap = mMemoryCache.get(url + width + height);
        if (bitmap != null) {
            setImageBitmap(imageView, bitmap, false);
            return;
        }
 
        // 生成文件名
        String filePath = urlToFilePath(url);
        if (filePath == null) {
            return;
        }
 
        queueImage(new ImageRef(imageView, url, filePath, resId, width, height));
    }
 
    /**
     * 入队,后进先出
     *
     * @param imageRef
     */
    public void queueImage(ImageRef imageRef) {
 
        // 删除已有ImageView
        Iterator<ImageRef> iterator = mImageQueue.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().imageView == imageRef.imageView) {
                iterator.remove();
            }
        }
 
        // 添加请求
        mImageQueue.push(imageRef);
        sendRequest();
    }
 
    /**
     * 发送请求
     */
    private void sendRequest() {
 
        // 开启图片加载线程
        if (mImageLoaderHandler == null) {
            HandlerThread imageLoader = new HandlerThread("image_loader");
            imageLoader.start();
            mImageLoaderHandler = new ImageLoaderHandler(
                    imageLoader.getLooper());
        }
 
        // 发送请求
        if (mImageLoaderIdle && mImageQueue.size() > 0) {
            ImageRef imageRef = mImageQueue.pop();
            Message message = mImageLoaderHandler.obtainMessage(MSG_REQUEST,
                    imageRef);
            mImageLoaderHandler.sendMessage(message);
            mImageLoaderIdle = false;
            mRequestQueue.add(imageRef);
        }
    }
 
    /**
     * 图片加载线程
     */
    class ImageLoaderHandler extends Handler {
 
        public ImageLoaderHandler(Looper looper) {
            super(looper);
        }
 
        public void handleMessage(Message msg) {
            if (msg == null)
                return;
 
            switch (msg.what) {
 
            case MSG_REQUEST: // 收到请求
                Bitmap bitmap = null;
                Bitmap tBitmap = null;
                if (msg.obj != null && msg.obj instanceof ImageRef) {
 
                    ImageRef imageRef = (ImageRef) msg.obj;
                    String url = imageRef.url;
                    if (url == null)
                        return;
                    // 如果本地url即读取sd相册图片,则直接读取,不用经过DiskCache
                    if (url.toLowerCase().contains("dcim")) {
 
                        tBitmap = null;
                        BitmapFactory.Options opt = new BitmapFactory.Options();
                        opt.inSampleSize = 1;
                        opt.inJustDecodeBounds = true;
                        BitmapFactory.decodeFile(url, opt);
                        int bitmapSize = opt.outHeight * opt.outWidth * 4;
                        opt.inSampleSize = bitmapSize / (1000 * 2000);
                        opt.inJustDecodeBounds = false;
                        tBitmap = BitmapFactory.decodeFile(url, opt);
                        if (imageRef.width != 0 && imageRef.height != 0) {
                            bitmap = ThumbnailUtils.extractThumbnail(tBitmap,
                                    imageRef.width, imageRef.height,
                                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
                            isFromNet = true;
                        } else {
                            bitmap = tBitmap;
                            tBitmap = null;
                        }
 
                    } else
                        bitmap = mDiskCache.get(url);
 
                    if (bitmap != null) {
                        // ToolUtil.log("从disk缓存读取");
                        // 写入map缓存
                        if (imageRef.width != 0 && imageRef.height != 0) {
                            if (mMemoryCache.get(url + imageRef.width
                                    + imageRef.height) == null)
                                mMemoryCache.put(url + imageRef.width
                                        + imageRef.height, bitmap);
                        } else {
                            if (mMemoryCache.get(url) == null)
                                mMemoryCache.put(url, bitmap);
                        }
 
                    } else {
                        try {
                            byte[] data = loadByteArrayFromNetwork(url);
 
                            if (data != null) {
 
                                BitmapFactory.Options opt = new BitmapFactory.Options();
                                opt.inSampleSize = 1;
 
                                opt.inJustDecodeBounds = true;
                                BitmapFactory.decodeByteArray(data, 0,
                                        data.length, opt);
                                int bitmapSize = opt.outHeight * opt.outWidth
                                        * 4;// pixels*3 if it's RGB and pixels*4
                                            // if it's ARGB
                                if (bitmapSize > 1000 * 1200)
                                    opt.inSampleSize = 2;
                                opt.inJustDecodeBounds = false;
                                tBitmap = BitmapFactory.decodeByteArray(data,
                                        0, data.length, opt);
                                if (imageRef.width != 0 && imageRef.height != 0) {
                                    bitmap = ThumbnailUtils
                                            .extractThumbnail(
                                                    tBitmap,
                                                    imageRef.width,
                                                    imageRef.height,
                                                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
                                } else {
                                    bitmap = tBitmap;
                                    tBitmap = null;
                                }
 
                                if (bitmap != null && url != null) {
                                    // 写入SD卡
                                    if (imageRef.width != 0
                                            && imageRef.height != 0) {
                                        mDiskCache.put(url + imageRef.width
                                                + imageRef.height, bitmap);
                                        mMemoryCache.put(url + imageRef.width
                                                + imageRef.height, bitmap);
                                    } else {
                                        mDiskCache.put(url, bitmap);
                                        mMemoryCache.put(url, bitmap);
                                    }
                                    isFromNet = true;
                                }
                            }
                        } catch (OutOfMemoryError e) {
                        }
 
                    }
 
                }
 
                if (mImageManagerHandler != null) {
                    Message message = mImageManagerHandler.obtainMessage(
                            MSG_REPLY, bitmap);
                    mImageManagerHandler.sendMessage(message);
                }
                break;
 
            case MSG_STOP: // 收到终止指令
                Looper.myLooper().quit();
                break;
 
            }
        }
    }
 
    /** UI线程消息处理器 */
    private Handler mImageManagerHandler = new Handler() {
 
        @Override
        public void handleMessage(Message msg) {
            if (msg != null) {
                switch (msg.what) {
 
                case MSG_REPLY: // 收到应答
 
                    do {
                        ImageRef imageRef = mRequestQueue.remove();
 
                        if (imageRef == null)
                            break;
 
                        if (imageRef.imageView == null
                                || imageRef.imageView.getTag() == null
                                || imageRef.url == null)
                            break;
 
                        if (!(msg.obj instanceof Bitmap) || msg.obj == null) {
                            break;
                        }
                        Bitmap bitmap = (Bitmap) msg.obj;
 
                        // 非同一ImageView
                        if (!(imageRef.url).equals((String) imageRef.imageView
                                .getTag())) {
                            break;
                        }
 
                        setImageBitmap(imageRef.imageView, bitmap, isFromNet);
                        isFromNet = false;
 
                    } while (false);
 
                    break;
                }
            }
            // 设置闲置标志
            mImageLoaderIdle = true;
 
            // 若服务未关闭,则发送下一个请求。
            if (mImageLoaderHandler != null) {
                sendRequest();
            }
        }
    };
 
    /**
     * 添加图片显示渐现动画
     *
     */
    private void setImageBitmap(ImageView imageView, Bitmap bitmap,
            boolean isTran) {
        if (isTran) {
            final TransitionDrawable td = new TransitionDrawable(
                    new Drawable[] {
                            new ColorDrawable(android.R.color.transparent),
                            new BitmapDrawable(bitmap) });
            td.setCrossFadeEnabled(true);
            imageView.setImageDrawable(td);
            td.startTransition(300);
        } else {
            imageView.setImageBitmap(bitmap);
        }
    }
 
    /**
     * 从网络获取图片字节数组
     *
     * @param url
     * @return
     */
    private byte[] loadByteArrayFromNetwork(String url) {
 
        try {
 
            HttpGet method = new HttpGet(url);
            HttpResponse response = myapp.getHttpClient().execute(method);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toByteArray(entity);
 
        } catch (Exception e) {
            return null;
        }
 
    }
 
    /**
     * 根据url生成缓存文件完整路径名
     *
     * @param url
     * @return
     */
    public String urlToFilePath(String url) {
 
        // 扩展名位置
        int index = url.lastIndexOf('.');
        if (index == -1) {
            return null;
        }
 
        StringBuilder filePath = new StringBuilder();
 
        // 图片存取路径
        filePath.append(myapp.getCacheDir().toString()).append('/');
 
        // 图片文件名
        filePath.append(MD5.Md5(url)).append(url.substring(index));
 
        return filePath.toString();
    }
 
    /**
     * Activity#onStop后,ListView不会有残余请求。
     */
    public void stop() {
 
        // 清空请求队列
        mImageQueue.clear();
 
    }
 
}

开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

 

发表评论
回到顶部

网友评论(44)

  • 41楼:木法沙王 发表于 2014-04-10 17:26
    回复此评论

    //如果本地url即读取sd相册图片,则直接读取,不用经过DiskCache
    if (url.toLowerCase().contains("dcim")

    建议把dcim改为mnt, 这样别的路经下图片也能加载了

  • 42楼:迷途的小羔羊 发表于 2014-05-02 16:14
    回复此评论

    我他妈的非常感谢你,真心的
  • 43楼:wgggfiy 发表于 2014-06-05 10:56
    回复此评论

    请问这个缓存何时可以清空呢??我担心内存和磁盘会慢慢越积越大
  • 44楼:巴顿将军 发表于 2014-07-19 18:16
    回复此评论

    什么东西呀,我跑不起来,看不到什么效果呢

开源从代码分享开始
分享代码

 
roc2013的其他代码

全部(6)...

 

Android图片管理组件(双缓存+异步加载)的更多相关文章

  1. Android中用双缓存技术,加载网络图片

    最近在学校参加一个比赛,写的一个Android应用,里面要加载大量的网络图片,可是用传统的方法图片一多就会造成程序出现内存溢出而崩溃.因为自己也在学习中,所以看了很多博客和视频,然后参照这些大神的写源 ...

  2. ANDROID_MARS学习笔记_S04_009_用java.lang.ref.SoftReference作缓存,android.os.Handler和new Thread异步加载略图片

    一.简介 二.代码流程 1.private Map<String, SoftReference<Drawable>> imageCache = new HashMap<S ...

  3. Android进阶:ListView性能优化异步加载图片 使滑动效果流畅

    ListView 是一种可以显示一系列项目并能进行滚动显示的 View,每一行的Item可能包含复杂的结构,可能会从网络上获取icon等的一些图标信息,就现在的网络速度要想保持ListView运行的很 ...

  4. Android之数据存储----使用LoaderManager异步加载数据库

    一.各种概念: 1.Loaders: 适用于Android3.0以及更高的版本,它提供了一套在UI的主线程中异步加载数据的框架.使用Loaders可以非常简单的在Activity或者Fragment中 ...

  5. Cocos2d-js 开发记录:图片数据资源等的异步加载

    这里说的是在需要的使用加载图片,比如游戏中的某个关卡的图片,不用在游戏一开始就加载(万一用户玩不到那关,岂不是很冤,流量费了那么多),否则载入速度也慢.这种方式加载资源要用到cc.loader官方文档 ...

  6. wemall app商城源码中基于JAVA的Android异步加载图片管理器代码

    wemall doraemon是Android客户端程序,服务端采用wemall微信商城,不对原商城做任何修改,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可随意定制修改.本文分享其中 ...

  7. android异步加载图片并缓存到本地实现方法

    图片过多造成内存溢出,这个是最不容易解决的,要想一些好的缓存策略,比如大图片使用LRU缓存策略或懒加载缓存策略.今天首先介绍一下本地缓存图片     在android项目中访问网络图片是非常普遍性的事 ...

  8. android实现异步加载图片类

    其中牵涉到的关键知识点 1,回调机制,不过回调接口的实现方式有多种多样,可以是一个类继承该接口,也可以是作为一个方法参数: 可以参照自己的这篇博客: http://www.cnblogs.com/bo ...

  9. 解决ListView滑动时卡的问题,实现异步加载图片解决

    ListView是最为常见的空间之一,现在的应用的呈现形式大多数都需要用到ListView来呈现,以列表的方式最直观最便于操作. 那么在使用的过程中大家一定使用adapter适配器来匹配这个ListV ...

随机推荐

  1. [java]struts2入门

    摘要 本文是struts2入门,配置教程.如何在IntelJ Idea中进行手动配置.在使用idea新建struts2web项目的时候,在下载jar包的过程中,下载失败,没办法就直接手动进行下载jar ...

  2. windows 启动关闭Oracle监听和服务

    经常要用数据库,让他自己启动的话,开机太慢,所以用命令启动方便点.    1.开启:     在运行中输入cmd,进入控制台,lsnrctl start回车,提示启动监听成功后 net start O ...

  3. Tomcat服务器配置https双向认证(使用keytool生成证书)

    一,HTTPS原理   1,HTTP.HTTPS.SSL.TLS介绍与相互关系 (1)HTTP:平时浏览网页时候使用的一种协议.HTTP协议传输的数据都是未加密的(明文),因此使用HTTP协议传输隐私 ...

  4. org.hibernate.QueryException: JPA-style positional param was not an integral ordinal; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: JPA-style positional param w

    org.hibernate.QueryException: JPA-style positional param was not an integral ordinal; nested excepti ...

  5. Postgres和MySQL创建用户并授予db权限

    Postgresql和MySQL还是有很多不同的.就比如授权来说.当下有个业务场景,我们的报表数据库需要根据业务划分不同的db,然后创建对应的user. 如果是MySQL, 可以这样做 mysql&g ...

  6. UVa 902 - Password Search

    题目:给你一个小写字母组成大的串和一个整数n.找到里面长度为n出现最频繁的子串. 分析:字符串.hash表.字典树. 这里使用hash函数求解,仅仅做一次扫描就可以. 说明:假设频率同样输出字典序最小 ...

  7. [Python设计模式] 第11章 迪米特法则——最少知识原则

    github地址:https://github.com/cheesezh/python_design_patterns 迪米特法则 迪米特法则(LoD),也叫最少知识原则,如果两个类不必彼此直接通信, ...

  8. Java通过JNI调用C++程序

    JNI是Java Native Interface的缩写,中文为JAVA本地调用.使用JNI可以很方便的用我们的Java程序调用C/C++程序.很多时候,某些功能用Java无法实现,比如说涉及到底层驱 ...

  9. PL/SQL学习笔记之变量、常量、字面量、字符串

    一:变量 1:变量声明与初始化 variable_name datatype(约束) [:= | DEFAULT 初始值] 如: sales , ); name ); a ; greetings ) ...

  10. about the libiconv.2.dylib

    https://stackoverflow.com/questions/5835847/libiconv-2-dylib-mac-os-x-problem https://blog.csdn.net/ ...