另一个 OutOfMemoryError:位图大小超出 VM 预算(由于透明度) - Android
我已经彻底搜索了几天,但没有得出太多结论。我读过的所有其他实例都涉及垃圾收集。
我正在做的是加载一个 4 KB PNG 图像,该图像基本上是完全透明的,带有白色的小“星星”。它是 1000x750。仅在程序启动时加载这一图像就会出现 OutOfMemoryError: 位图大小超出 VM 预算和 E/dalvikvm-heap(3941): 27000000 字节外部分配对于此进程来说太大。
使用完全相同的代码,我可以毫无问题地加载没有透明度的 176KB PNG 背景图像。这只是一个简单的“stars1 = BitmapFactory.decodeResource(getResources(), R.drawable.back3c);”
那么,解码透明 PNG 会占用大量内存,这是否存在某种关联?除了使用较小的图像之外,还有什么方法可以解决这个问题吗?
如果我将图像缩小到 500x375,它可以正常加载。我想我只是想知道是否有更好的方法来处理透明图像(如果这是问题的话)。
该代码几乎直接是 LunarLander 示例。实际解码位图的代码:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
options.inSampleSize = 12;
stars1 = BitmapFactory.decodeResource(getResources(), R.drawable.back4e);
//stars1 = context.getResources().getDrawable(R.drawable.back3c);
//stars2 = context.getResources().getDrawable(R.drawable.back4b);
BackgroundImage = context.getResources().getDrawable(R.drawable.back2);
I've searched on this thoroughly for a few days and haven't come up with much. Every other instance I have read deals with garbage collection.
What I'm doing is loading a 4 KB PNG image which is basically completely transparent with small white "stars". It is 1000x750. Just loading this one image when the program starts gives an OutOfMemoryError: bitmap size exceeds VM budget and E/dalvikvm-heap(3941): 27000000-byte external allocation too large for this process.
With the exact same code I have no problem loading a 176KB PNG background image with no transparency. It's just a simple "stars1 = BitmapFactory.decodeResource(getResources(), R.drawable.back3c);"
So is there some correlation where decoding a transparent PNG takes up an insane amount of memory? And is there any way to fix this other than using a smaller image?
If I scale the image down to 500x375 it loads fine. I guess I'm just wondering if there is a better way to deal with a transparent image if that is the issue.
The code is almost directly the LunarLander example. Code actually decoding the bitmap:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
options.inSampleSize = 12;
stars1 = BitmapFactory.decodeResource(getResources(), R.drawable.back4e);
//stars1 = context.getResources().getDrawable(R.drawable.back3c);
//stars2 = context.getResources().getDrawable(R.drawable.back4b);
BackgroundImage = context.getResources().getDrawable(R.drawable.back2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论