gpt4 book ai didi

android - 如何在不超过 Android 中的 VM 限制的情况下在 tabhost 中允许多个位图画廊?

转载 作者:行者123 更新时间:2023-11-29 14:04:39 25 4
gpt4 key购买 nike

在我的主要 Activity 中(将 launchmode 设置为 singleTask ),我有一个带有三个选项卡的 TabHost,它们都包含一个自定义 Gallery,显示 Views 包含一个存储在本地的 bitmap 图像(一旦我们有服务器支持,这些将是而是下载)。

我遇到的问题是 java.lang.OutOfMemoryError: bitmap size exceeds VM budget 错误(在我的模拟器上。它在我的 Galaxy Tab 上运行得非常好)。 I understand that this means I am using too many bitmaps of large sizes without recycling them, however I am having difficulty figuring out the best place to recycle them, since each tab remains running in the background when another tab is selected.

我是否需要将我的 Gallery 适配器更改为一次只加载几个位图(例如选定的、左 View 和右 View ),或者是否有更简单的解决方案(例如,调用 recycle() 的好地方) ?

编辑:我尝试使用此代码,并在选择新的画廊项目时调用它。它起初有效,但后来应用程序仍然崩溃。我没有正确调用 recycle() 吗?

/* *
* Set the current, left, and right view's images. Set all others to NULL and recycle them.
*/
public void refreshImages() {
for (int i = 0; i < adapter.getCount(); i++) {
View v = adapter.getView(i, null, null);
ImageView img = (ImageView) v.findViewById(R.id.add_image);
Thing mything = adapter.getThing(i);//returns a Thing object at this position in the gallery.
if (i == currentGalleryPosition //This is set when the gallery item is selected.
|| i == (currentGalleryPosition - 1)
|| i == (currentGalleryPosition + 1)) {
img.setImageBitmap(thing.getImage(this)); //This retrieves the bitmap from my drawable resources.
img.setScaleType(ImageView.ScaleType.FIT_XY);
}
else {
Bitmap bmp = thing.getImage();
img.setImageBitmap(null);
bmp.recycle();
}
}
}

最佳答案

也许 System.gc(); 。用它来调用垃圾收集器。

一个提示:每当有人点击一个选项卡,或者当您传递例如 10 张图片时,我都会使用它。

关于android - 如何在不超过 Android 中的 VM 限制的情况下在 tabhost 中允许多个位图画廊?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7760241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com