Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

How can I convert Java bitmap to byte array?

Asked by Kayla Shinn Feb 5, 2021 1.5K views 1 answer
Share

About this question

 Bitmap bmp   = intent.getExtras().get("data");

  int size     = bmp.getRowBytes() * bmp.getHeight();

  ByteBuffer b = ByteBuffer.allocate(size);

  bmp.copyPixelsToBuffer(b);

  byte[] bytes = new byte[size];

  try {

     b.get(bytes, 0, bytes.length);

  } catch (BufferUnderflowException e) {

     // always happens

  }

  // do something with byte[]

When I look at the buffer after the call to copyPixelsToBuffer the bytes are all 0... The bitmap returned from the camera is immutable... but that shouldn't matter since it's doing a copy.

What could be wrong with this code?

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.