如何允许在tabhost多个位图画廊不超过android系统中的虚拟机限制? [英] How do I allow multiple bit-map galleries in a tabhost without exceeding the VM limit in android?

查看:111
本文介绍了如何允许在tabhost多个位图画廊不超过android系统中的虚拟机限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要活动(与​​ launchmode 设置为 singleTask ),我有一个 TabHost 有三个选项卡,这都包含一个自定义的图库,其中显示查看包含<这是本地存储(一旦我们服务器的支持,它们将被代替下载)。code>位图图片

我遇到的问题是 java.lang.OutOfMemoryError:(我的模拟器它的工作原理了不起我Galaxy Tab的)位图的大小超过VM预算错误。据我所知,这意味着我使用了太多的位图大尺寸,而无需重复它们,但我有困难找出最好的地方,回收他们,因为每个标签保持运行在后台时,选择另一个选项卡。

我需要我的画廊适配器更改为只能有一次装几个位图(如选择左,右视图),还是有一个简单的解决方案(例如,一个好地方,叫循环())?

编辑:
我试图用这个code,并把它称为当选择一个新的画廊项目。它的工作在第一,但随后还是应用程序崩溃。我不能叫再循环()是否正确?

  / * *
 *设置当前,左,右视图的图像。将所有其他为NULL,并回收。
 * /
公共无效refreshImages(){
    的for(int i = 0; I&LT; adapter.getCount();我++){
        视图V = adapter.getView(I,NULL,NULL);
        ImageView的IMG =(ImageView的)v.findViewById(R.id.add_image);
        事情mything = adapter.getThing(我); //在画廊这个位置返回Thing对象。
        如果(我== currentGalleryPosition //这是设置在选择库项目。
         ||我==(currentGalleryPosition - 1)
         ||我==(currentGalleryPosition + 1)){
            img.setImageBitmap(thing.getImage(本)); //这个检索我的绘图资源的位图。
            img.setScaleType(ImageView.ScaleType.FIT_XY);
        }
        其他{
            BMP位图= thing.getImage();
            img.setImageBitmap(NULL);
            bmp.recycle();
        }
    }
}


解决方案

这是不是最好的解决方案,但它做什么,我需要做的:

在解码一个位图,设置inPurgeable和inInputShareable选项设置为true。这使得它,这样,如果虚拟机需要更多空间,它可以简单地删除图像(清除),但如果你是其中包括多个选项卡共享多个图像,inInputShareable选项允许去codeR简单地共享内存中的相同位置,而不会产生相同的位图的新实例

  BitmapFactory.Options O =新BitmapFactory.Options();
o.inDither = TRUE;
o.inPurgeable = TRUE;
o.inInputShareable = TRUE;
返回BitmapFactory.de codeResource(getResources(),R.drawable.img,O);

In my main activity (with launchmode set as singleTask), I have a TabHost with three tabs, which all contain a custom Gallery that displays Views containing a bitmap image which is stored locally (once we have server support, these will be downloaded instead).

The problem I am running into is the java.lang.OutOfMemoryError: bitmap size exceeds VM budget error (on my emulator. It works terrific on my 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.

Do I need to change my Gallery adapter to only have a few bitmaps loaded at once (such as the selected, left, and right views), or is there a simpler solution (for example, a good place to call recycle())?

Edit: I tried using this code, and called it when a new gallery item is selected. It worked at first, but then the app still crashed. Am I not calling recycle() correctly?

/* *
 * 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();
        }
    }
}

解决方案

This is not the best solution, but it does what I need it to do:

When decoding a bitmap, set the inPurgeable and inInputShareable options to true. This makes it so that if the VM needs more space, it can simply remove images (purge), but if you are sharing multiple images amongst multiple tabs, the inInputShareable option allows the decoder to simply share the same location in memory, without creating a new instance of the same bitmap.

BitmapFactory.Options o = new BitmapFactory.Options();
o.inDither = true;
o.inPurgeable = true;
o.inInputShareable = true;
return BitmapFactory.decodeResource(getResources(), R.drawable.img, o);

这篇关于如何允许在tabhost多个位图画廊不超过android系统中的虚拟机限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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