【问题标题】:Android ListView OutOfMemoryError: bitmap size exceeds VM budget [closed]Android ListView OutOfMemoryError:位图大小超出 VM 预算 [关闭]
【发布时间】:2012-06-20 05:40:12
【问题描述】:

好的,这是一个非常常见的问题,但我的问题有点不同,我找不到其他主题的解决方案,所以我在这里发布一个新问题。我有一个显示 ListView 的应用程序。 ListView 的每一行,我都有一个 ImageView 来使用 ListAdapter 从 SD 卡加载一个小位图图标(它很小,所以问题不在于大小)。现在,如果我慢慢滚动列表,它工作正常。但如果我滚动得非常快,当 ListView 足够长时,它就不再显示图标,logcat 中的消息是这样的:

126 600-byte external allocation too large for this process.

VM 不会让我们分配 126,600 字节

然后app crash,logcat显示:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

我在 2 台不同的设备上进行了测试,其中只有 1 台出现此错误,其他的正常。 请注意,此错误仅在 ListView 滚动非常快时发生。那是因为创建的新线程与垃圾收集的速度不匹配吗? 在这种情况下有人可以给我一些建议吗?

【问题讨论】:

  • @Paresh Mayani:那是什么意思?我真的是 android 新手,所以请更具体一点
  • 提问时,请先搜索一下,这里已经有很多问题了:OutOfMemoryError: bitmap size exceeds VM budget
  • 检查屏幕右上角的搜索框。它会回答你的!!
  • 当然我已经搜索过其他主题,但它们没有帮助,这就是我必须发布新主题的原因。我在帖子里说过。
  • 是否可以在 ListAdapter 的 getView 中发布代码?,您应该重用传递的视图并使用 setTag 保留位图的引用并在设置为新的位图之前回收位图。

标签: android listview bitmap out-of-memory


【解决方案1】:

在使用位图时,与 ListView 一起使用时,您通常会得到 OutOfMemory。

所以你应该选择LasyList,如图所示。

Will 将负责您的图片加载。

要使用 SDCard,您需要替换 ImageLoader 类中的方法,如下所示

private Bitmap getBitmap(String url) 
    {
        // If is from SD Card
        try {
            File file = new File(url);
            if(file.exists())
            {
                return BitmapFactory.decodeFile(url);
            }
        } catch (Exception e) {

        }

        File f=fileCache.getFile(url);

        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null)
            return b;

        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Exception ex){
           ex.printStackTrace();
           return null;
        }
    }

【讨论】:

  • 我的方法和你的一样,没有解决问题
猜你喜欢
  • 2010-12-07
  • 1970-01-01
  • 2011-02-25
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
相关资源
最近更新 更多