Java RuntimeException: 缓冲区不足以容纳像素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22167192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:02:17  来源:igfitidea点击:

RuntimeException: Buffer not large enough for pixels

javaandroidbitmap

提问by WWJD

I'm receiving a Bitmap in byte array through socket and I read it and then I want to set it os.toByteArrayas ImageViewin my application. The code I use is:

我通过socket接收的字节数组位图,我读了它,然后我想将其设置os.toByteArrayImageView我的应用程序。我使用的代码是:

try {
    //bmp = BitmapFactory.decodeByteArray(result, 0, result.length);
    bitmap_tmp = Bitmap.createBitmap(540, 719, Bitmap.Config.ARGB_8888);
    ByteBuffer buffer = ByteBuffer.wrap(os.toByteArray());

    bitmap_tmp.copyPixelsFromBuffer(buffer);
    Log.d("Server",result+"Length:"+result.length);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            imageView.setImageBitmap(bitmap_tmp);
        }
    });
    return bmp;
} finally {
}

When I run my application and start receiving Byte[]and expect that ImageViewis changed, it's not.

当我运行我的应用程序并开始接收Byte[]并期望ImageView更改时,事实并非如此。

LogCatsays:

LogCat说:

java.lang.RuntimeException: Buffer not large enough for pixels at
android.graphics.Bitmap.copyPixelsFromBuffer

I searched in similar questions but couldn't find a solution to my problem.

我搜索了类似的问题,但找不到解决我的问题的方法。

采纳答案by CCJ

Take a look at the source (version 2.3.4_r1, last time Bitmap was updated on Grepcode prior to 4.4) for Bitmap::copyPixelsFromBuffer()

看一下Bitmap::copyPixelsFromBuffer()的源代码(版本 2.3.4_r1,上次 Bitmap 在 4.4 之前的 Grepcode 上更新

The wording of the error is a bit unclear, but the code clarifies-- it means that your buffer is calculated as not having enough data to fill the pixels of your bitmap. This is (possibly) because they use the buffer's remaining() method to figure the capacity of the buffer, which takes into account the current value of its position attribute. If you call rewind() on your buffer before you invoke copyFromPixels(), you might see the runtime exception disappear. I say 'might' because the ByteBuffer::wrap() method should set the position attribute value to zero, removing the need to call rewind, but judging by similar questions and my own experience resetting the position explicitly may do the trick.

错误的措辞有点不清楚,但代码澄清了 - 这意味着您的缓冲区被计算为没有足够的数据来填充位图的像素。这是(可能)因为他们使用缓冲区的剩余()方法来计算缓冲区的容量,这考虑了其位置属性的当前值。如果在调用 copyFromPixels() 之前在缓冲区上调用 rewind(),您可能会看到运行时异常消失。我说“可能”是因为 ByteBuffer::wrap() 方法应该将 position 属性值设置为零,从而消除调用 rewind 的需要,但是从类似的问题和我自己的经验来看,明确重置位置可能会成功。

Try

尝试

ByteBuffer buffer = ByteBuffer.wrap(os.toByteArray());
buffer.rewind();
bitmap_tmp.copyPixelsFromBuffer(buffer);

回答by Jakub Czupryna

The buffer size should be exactly 1553040B (assuming bitmap's height, width and 32bit to encode each color).

缓冲区大小应该正好是 1553040B(假设位图的高度、宽度和 32 位来编码每种颜色)。