RuntimeException: Buffer not large enough for pixels

后端 未结 2 2010
耶瑟儿~
耶瑟儿~ 2021-01-13 08:38

I\'m receiving a Bitmap in byte array through socket and I read it and then I want to set it os.toByteArray as ImageView in my application. The cod

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-13 09:26

    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()

    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.

    Try

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

提交回复
热议问题