Convert bitmap to byte array in Java

Asked 1 years ago, Updated 1 years ago, 95 views

  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) {
     //Exception handling
  }
  // Byte[] Processing

What's wrong with Ekid? Why isn't it moving?

java android serialization bitmap bytebuffer

2022-09-21 15:31

1 Answers

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Do it like this.


2022-09-21 15:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.