许多图像定制的ListView得到内存溢出异常 [英] Custom ListView with many images gets Out Of Memory Exception

查看:182
本文介绍了许多图像定制的ListView得到内存溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个Android的ListView将看起来像Instagram的图片饲料或Facebook的。加载一些图像(只有几个)后,我要走了我的Galaxy Nexus的内存的异常。我缩放他们,不过,图像的数量是不固定的并在理论上可以是无限的。在Facebook和Instagram他们加载大量的图片和其他数据,并且效果很好。

I'm trying to create an Android ListView that will look like Instagram pictures feed or Facebook's. I'm getting Out Of Memory Exception on my Galaxy Nexus after loading some images (only a few). I scaled them, but still, the number of images isn't fixed and theoretically can be unlimited. On Facebook and Instagram they load lots of images and other data and it works well.

你觉得呢?我能达到与原生的Andr​​oid编码的表演?
如果我将创建一个HTML5饲料(如Facebook和Instagram)将我避免了内存的问题?

What do you think? Can i achieve those performances with native Android coding? If i'll create a HTML5 feed (like Facebook and Instagram) will i avoid the memory problem?

这感觉怪怪的,我说我需要做这么多工作处理来自服务器未来图像的记忆。如果这是唯一的解决办法是什么去原生的ListView管理上百张图片的方式?

It feels weird for me that I need to do so much work handling the memory of images coming from server. If it's the only solution, what is the way to go for managing hundreds of pictures in native ListView?

我将AP preciate任何意见!
谢谢你。

I will appreciate any opinion! Thanks.

推荐答案

不要将它们全部加载到内存中一次,只加载需要显示成位图对象;并确保您加载图像只需要显示(没有完全加载1900x1200的图像显示缩略图安全工程师)的大小。您的应用程序的堆大小为〜30MB(设备相关的,但是这是一个很好的经验法则),和一个位图会占用宽*高* 4 在内存中的字节一旦加载。

Don't load them all into memory at once, only load the images you need to display into Bitmap objects; and make sure the images you load are only the size you need to display (don't fully load a 1900x1200 image to show a 300x200 thumbnail). Your application's heap size is ~30MB (device dependent, but that's a good rule of thumb), and a Bitmap will take up Width*Height*4 bytes in memory once loaded.

的一种方法是将图像缓存到磁盘上,因为它们被下载,以便它们不必须是在内存中,并且总是在下载前检查的图像的高速缓存。也可以使用高速缓存的图像的少数加载到内存需要在该时间要显示在列表中。

One method is to cache the images onto disk as they are downloaded so they don't have to be in memory, and always check the cache for an image before downloading it. You can also use the cache to load up a handful of images into memory that need to be displayed in the list at that time.

您还可以使用的 BitmapFactory.Options.inSampleSize 选项减小图像的大小,同时它被加载,而不是整个图像中的装载,然后创建一个小型副本,浪费时间和内存。

You can also make use of the BitmapFactory.Options.inSampleSize options to reduce the size of an image while it is loaded, rather than loading in the entire image and then creating a small copy, which wastes time and memory.

心连心

看看: http://developer.android.com/training求助/displaying-bitmaps/index.html

这篇关于许多图像定制的ListView得到内存溢出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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