简体   繁体   English

Android ListView显示图像结果内存不足

[英]Android ListView showing images results in out of memory

Okay, 好的,

I have what I think is an odd problem. 我有一个奇怪的问题。

I load photos into a list view as the user scroll (meaning if picked up from disk or online, it takes a little time and they "pop in") 当用户滚动时,我将照片加载到列表视图 (这意味着从磁盘或在线拾取照片会花费一些时间,它们会“弹出”)

1) 1)

I ensure that I only fetch one image online + resize to 1/3 width at a time (a lock) -- this is to ensure not eg 5 threads each are converting a .jpg to bitmap at the same time. 我确保只一次在线获取一张图像+一次将大小调整为1/3宽度 (锁定)-这是为了确保例如5个线程不会同时将.jpg转换为位图。 (Imagine a user scrolling very fast, it would then be conceivable this could happen.) (假设用户滚动非常快,那么可以想象会发生这种情况。)

2) 2)

All images on disk are only 1/3 width 磁盘上的所有图像只有1/3宽度

3) 3)

While the bitmaps are scaled up to fit device width, I call recycle and set bitmaps to null when used in the getView . 当位图按比例缩放以适合设备宽度时,我调用recycle并将在getView中使用的位图设置为null。 (I am converting bitmaps to a BitmapDrawable object which the listitem imageview uses in a setImageDrawable call) (我将位图转换为Listitem imageviewsetImageDrawable调用中使用的BitmapDrawable对象)

4) 4)

I reuse view in getView if not null 如果不为null,我会在getView中重用视图

5) 5)

I load bitmaps like this: 我加载这样的位图:

BitmapFactory.Options o = new BitmapFactory.Options();   
o.inPurgeable = false; // I do not use this since I resize/scale down images myself later
o.inInputShareable = false;
o.inPreferredConfig = Config.RGB_565;
res.bitmap = BitmapFactory.decodeStream(is, null, o);

6) 6)

I also clean scrap items 我也清理废料

public void onMovedToScrapHeap(View view) {
          final TextView description = (TextView)view.findViewById(R.id.listitem_news_content);
          description.setText("");
          final ImageView image = (ImageView)view.findViewById(R.id.listitem_news_image);
          image.setImageBitmap(null);                                            
        }

I am testing on a Samsung Galaxy II. 我正在三星银河II上进行测试。 Do you guys have any ideas on what more I could try? 你们对我可以尝试的方法有任何想法吗? Since I only max need to show 10 items, I would think it should be possible... 由于我最多只需要显示10个项目,所以我认为应该可以...

Try using widely used library Universal Image Loader https://github.com/nostra13/Android-Universal-Image-Loader 尝试使用广泛使用的库Universal Image Loader https://github.com/nostra13/Android-Universal-Image-Loader

It is simple and straight forward and you will never have to care about decoding images yourself. 这很简单明了,您无需担心自己解码图像。

Option two: There's a powerful new API for loading image is available from the team of Square http://square.github.io/picasso/ 选项二:Square的团队提供了一个功能强大的新API,用于加载图像: http://square.github.io/picasso/

Try this, it worked for me in case of OutOfMemory...using System.gc() 尝试一下,在使用OutofMemory的情况下对我有用...使用System.gc()

bitmap = null;
imageView.setImageBitmap(null);
System.gc();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM