java.lang.OutOfMemoryError:位图大小超过了列表视图中的 VM 预算



我在 https://stackoverflow.com/a/6779067/1236259 线程中尝试了hp.android答案,但我的活动从未调用onDestroy方法

有一个带有图像的列表视图,我通过以下方式调用其他活动:

ImageButton imageButtonSeeMap = (ImageButton)this.findViewById(R.id.imageButtonSeeMap);
imageButtonSeeMap.setOnClickListener(new OnClickListener() {....

从新活动中,我再次调用ListView活动,在这一点上,我得到了解释:java.lang.OutOfMemory错误:位图大小超过VM预算

列表视图中的图像是使用以下方法创建的:

thumb_u = new URL(lp.get(i).getImage());
            thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
            imageViewPoi.setImageDrawable(thumb_d);

如何释放图像的内存?从不调用 onDestroy 方法中的 unbindDrawables。

知道吗?

图像在 android 中是野兽,所以内存不足异常是你可能不得不习惯的事情。 这应该允许您的列表视图从 Web 获取图像,而不是使应用程序崩溃。

假设第二个代码块来自列表视图的适配器 getView() 方法,请尝试:

try {
    thumb_u = new URL(lp.get(i).getImage());
    thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
    imageViewPoi.setImageDrawable(thumb_d);
} catch (OutOfMemoryError e) {
    System.gc();
    thumb_u = new URL(lp.get(i).getImage());
    thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
    imageViewPoi.setImageDrawable(thumb_d);
}

最新更新