Android的懒加载图像类吃了我所有的记忆 [英] Android lazy loading images class eats up all my memory

查看:139
本文介绍了Android的懒加载图像类吃了我所有的记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用发现流行的延迟加载类here:http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012

我遇到的问题是,我使用这个类在多个活动等..导航来回活动(加载图像)的最终崩溃我的应用程序,给我这个错误:

  19 12-07:54:42.414:W / dalvikvm(1204):主题ID = 91:螺纹未捕获的异常退出(组= 0x4001b188)
12-07 19:54:42.424:E / AndroidRuntime(1204):未捕获的处理程序:螺纹螺纹-47退出,由于未捕获的异常
12-07 19:54:42.434:E / AndroidRuntime(1204):java.lang.OutOfMemoryError:位图大小超过VM预算
12-07 19:54:42.434:E / AndroidRuntime(1204):在android.graphics.BitmapFactory.nativeDe codeStream(本机方法)
12-07 19:54:42.434:E / AndroidRuntime(1204):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:459)
12-07 19:54:42.434:E / AndroidRuntime(1204):在de.tablayoutexample.ImageLoader.de codeFILE(ImageLoader.java:124)
12-07 19:54:42.434:E / AndroidRuntime(1204):在de.tablayoutexample.ImageLoader.getBitmap(ImageLoader.java:78)
12-07 19:54:42.434:E / AndroidRuntime(1204):在de.tablayoutexample.ImageLoader.access $ 0(ImageLoader.java:73)
12-07 19:54:42.434:E / AndroidRuntime(1204):在de.tablayoutexample.ImageLoader $ PhotosLoader.run(ImageLoader.java:182)

下code是ImageLoader的类的一部分,是的,我怀疑的罪魁祸首。
最初,REQUIRED_SIZE被设定为70,这​​是太小。我将它设置为200,这使得图像质量更好,但崩溃的应用程序更快。

不如果这种延迟加载方法清洗每次用户离开活动时间的图像?这似乎是刚刚加入到桩每一次我在另一个活动加载更多的图像。

我是新到Android编程,因此,或许有人可以帮我优化这个code。

  //德$ C $连拍影像的鳞它来减少内存消耗
    私人位图德codeFILE(文件f){
        尝试{
            //德code图像尺寸
            BitmapFactory.Options O =新BitmapFactory.Options();
            o.inJustDe codeBounds = TRUE;
            BitmapFactory.de codeStream(新的FileInputStream(F),空,O);            //找到正确的比例值。它应该是2的幂。
            最终诠释REQUIRED_SIZE = 200;
            INT width_tmp = o.outWidth,height_tmp = o.outHeight;
            int标= 1;
            而(真){
                如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2版; REQUIRED_SIZE)
                    打破;
                width_tmp / = 2;
                height_tmp / = 2;
                规模* = 2;
            }            //德code。与inSampleSize
            BitmapFactory.Options O2 =新BitmapFactory.Options();
            o2.inSampleSize =规模;
            返回BitmapFactory.de codeStream(新的FileInputStream(F),空,O2);
        }赶上(FileNotFoundException异常五){}
        返回null;
    }


解决方案

的WeakHashMap可以帮助ü缓存位图。
http://developer.android.com/reference/java/util/WeakHashMap.html

I'm using the popular lazy loading class found here:http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012

The problem I'm having is that I'm using this class in multiple activities etc... navigating back and forth between activities(loading images) eventually crashes my app, giving me this error:

12-07 19:54:42.414: W/dalvikvm(1204): threadid=91: thread exiting with uncaught exception (group=0x4001b188)
12-07 19:54:42.424: E/AndroidRuntime(1204): Uncaught handler: thread Thread-47 exiting due to uncaught exception
12-07 19:54:42.434: E/AndroidRuntime(1204): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
12-07 19:54:42.434: E/AndroidRuntime(1204):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-07 19:54:42.434: E/AndroidRuntime(1204):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
12-07 19:54:42.434: E/AndroidRuntime(1204):     at de.tablayoutexample.ImageLoader.decodeFile(ImageLoader.java:124)
12-07 19:54:42.434: E/AndroidRuntime(1204):     at de.tablayoutexample.ImageLoader.getBitmap(ImageLoader.java:78)
12-07 19:54:42.434: E/AndroidRuntime(1204):     at de.tablayoutexample.ImageLoader.access$0(ImageLoader.java:73)
12-07 19:54:42.434: E/AndroidRuntime(1204):     at de.tablayoutexample.ImageLoader$PhotosLoader.run(ImageLoader.java:182)

The code below is part of the ImageLoader class and is, I suspect, the culprit. Originally, REQUIRED_SIZE is set at 70, which is way too small. I set it at 200 which makes the image quality better, but crashes the app sooner.

Shouldnt this lazy loading method be cleaning the images every time the user leaves an activity? It seems it is just adding onto the pile every single time I load more images in another activity.

I'm new to android programming, so perhaps someone could help me optimize this code.

 //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=200;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

解决方案

WeakHashMap may help u to cache bitmap. http://developer.android.com/reference/java/util/WeakHashMap.html

这篇关于Android的懒加载图像类吃了我所有的记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆