所以我明白了:
'01-01 20:37:34.859: E/dalvikvm-heap(19921): Out of memory on a 6471856-byte allocation.'
当试图将一堆变量分配给内存时。 我正在分配 24 个这样的变量:
mElements.add(new ShopElement(getResources(),R.drawable.shop_starter, b1X , b1Y,true,1,"red",true,5,checkLocked(0)));
其中之一:
bNumbers = new Bitmap[] {Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h0)
, 65,65, true),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h1)
, 65,65, true),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h2)
, 65,65, true),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h3)
, 65,65, true),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h4)
,65,65,true),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h5)
, 65,65, true),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h6)
, 65,65, true),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h7)
, 65,65, true),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h8)
, 65,65, true),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h9)
, 65,65, true),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.time)
, 50,50, true)};
但在我完成创建的 24 个元素中的第 16 个元素之前,它在某些手机上崩溃了。
我有一个 Galaxy S2,它运行良好,但在 S3 和其他手机上它会崩溃。
我很乐意根据要求提供更多信息。
我之前搜索过这个错误,尝试应用解决方案,并查看了“有效显示位图”的内容,但似乎没有任何帮助。
令我头晕的是它适用于 S2,但不适用于 S3。 S3 不应该在所有方面都超过 S2 吗?
我正在使用全局 BitmapFactory.Options 来绘制我的所有位图,我将选项设置如下:
Global.opts = new BitmapFactory.Options();
Global.opts.inDither=false;
Global.opts.inPurgeable=true;
Global.opts.inInputShareable=true;
Global.opts.inTempStorage=new byte[16 * 1024];
最佳答案
只是您正在尝试访问比您拥有的更多的内存。因此,解决方案是增加内存或仅使用可用内存。
这样做的三个建议。
1) 如果您正在使用 BitmapFactory.Options
并且仍然存在错误,那么这意味着您没有为给定的图像使用正确的配置。 只需确保堆大小足够。
BitmapFactory.Options opts=new BitmapFactory.Options();
opts.inDither=false; //Disable Dithering mode
opts.inSampleSize = 8; // scale image upto 1/8 of the original image.
opts.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
opts.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
opts.inTempStorage=new byte[16 * 1024]; //you can increase this value as per your need.
您可以增加 inTempStorage
大小以使其工作或使用 inSampleSize
属性对图像进行采样
。关于采样的更多信息,您可以引用这个 link .
尝试使用不同的选项。它应该有效。
2) 您还可以使用 android:largeHeap="true" 增加应用程序的整体堆大小manifest
文件中的属性,但这不适用于任何 pre Honeycomb 设备。在 2.3 之前的设备上,您可以使用 VMRuntime 类。
3) 如果您想以编程方式检测堆大小并根据需要更改它,那么您可以按照此 link 进行操作.
关于Android分配变量时内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14116757/