RuntimeException: Buffer not large enough for pixels

后端 未结 2 2000
耶瑟儿~
耶瑟儿~ 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);
    
    0 讨论(0)
  • 2021-01-13 09:29

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

    0 讨论(0)
提交回复
热议问题