Bitmap.createBitmap(int, int, Bitmap.Config) 在类膨胀期间抛出运行时异常
我正在尝试扩展 Android 中的可扩展图像查看器以并排使用两个图像。为此,我使用 Bitmap.createBitmap(int, int, Bitmap.Config)。不幸的是,这似乎导致虚拟机崩溃。
代码是
protected void combineBitmaps() {
image0Width = bitmap0.getWidth();
image0Height = bitmap0.getHeight();
image1Width = bitmap1.getWidth();
image1Height = bitmap1.getHeight();
//These methods just get correct values for image[0|1][Width|Height].
int width = getCanvasWidth();
int height = getCanvasHeight();
//THIS LINE CRASHES
Bitmap bitmap_tmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap = bitmap_tmp;
Canvas combination = new Canvas(bitmap);
combination.drawBitmap(bitmap0, 0f, getImage0VertOffset(), null);
combination.drawBitmap(bitmap1, image0Width, getImage1VertOffset(), null);
}
我从 getCanvasWidth()
获得的值是 2464,从 getCanvasHeight()
获得的值是 2048。我得到的异常是
Unable to start activity ComponentInfo{com.tse.scview/com.tse.scview.ScalableViewWrapper}: android.view.InflateException: Binary XML file line #8: Error inflating class com.tse.scview.ScalableFacingPageView
com.tse. scview.ScalableFacingPageView
是我们正在构造的类(构造函数调用此函数)。
我已经将这一行中的所有内容(将位图分配给类位图、获取宽度和高度函数等)隔离开来,以确定实际上是 Bitmap.createBitmap 导致了问题。不幸的是我无法弄清楚问题是什么。
更新:作为健全性检查,我输入了较小的宽度和高度值(12 和 13...为什么不呢)。现在可以了。所以这是一个 RAM 问题,这对我们来说是一个痛苦,但有效地回答了这个问题。
I am trying to extend a scalable image viewer in Android to use two images side-by side. To do this I am using Bitmap.createBitmap(int, int, Bitmap.Config)
. Unfortunately this seems to be crashing the VM.
The code is
protected void combineBitmaps() {
image0Width = bitmap0.getWidth();
image0Height = bitmap0.getHeight();
image1Width = bitmap1.getWidth();
image1Height = bitmap1.getHeight();
//These methods just get correct values for image[0|1][Width|Height].
int width = getCanvasWidth();
int height = getCanvasHeight();
//THIS LINE CRASHES
Bitmap bitmap_tmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap = bitmap_tmp;
Canvas combination = new Canvas(bitmap);
combination.drawBitmap(bitmap0, 0f, getImage0VertOffset(), null);
combination.drawBitmap(bitmap1, image0Width, getImage1VertOffset(), null);
}
The value I am getting from getCanvasWidth()
is 2464 and from getCanvasHeight()
is 2048. The exception I am getting is
Unable to start activity ComponentInfo{com.tse.scview/com.tse.scview.ScalableViewWrapper}: android.view.InflateException: Binary XML file line #8: Error inflating class com.tse.scview.ScalableFacingPageView
where com.tse.scview.ScalableFacingPageView
is the class we're constructing (the constructor calls this function).
I've isolated everything from this line—the assignment of the bitmap to a class bitmap, the get width and height functions etc.—to determine that it IS in fact Bitmap.createBitmap that is causing the problem. Unfortunately I can't work out what the problem is.
Update: as a sanity check I put in small values for the width and height (12 and 13 ... well why not). It now works. So it's a RAM problem, which is a pain for us, but effectively answers the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确地阅读您的代码和问题,则位图为 2464 x 2048 x 4 字节。那是 16 兆字节。您正在创建其中两个。那是 32 兆字节。
If I am reading your code and question correctly, the bitmap is 2464 x 2048 x 4 bytes. That's 16 megabytes. And you are creating two of them. That's 32 megabytes.
我读到,如果您使用 OpenGL 纹理,则可以为位图分配更多内存,因为这不计入 Android 每个应用程序的最大内存限制。另一种方法是在 ndk 中分配内存,这也不会计入 Android 每个应用程序的最大内存限制。
I've read that you can allocate more memory for Bitmaps if you use an OpenGL texture, since that doesn't count towards Android's maximum memory per app restriction. Another way is allocating memory in the ndk which also doesn't count towards Android's maximum memory per app restriction.