0

Can someone please tell how I can convert this bitmap to byteArray?

Here is the code

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            // Log.d(TAG, String.valueOf(bitmap));
            EventBus.getDefault().post(new MessageEvent.SendMessage(contact.getJid(), null, ""));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
nsamad400
  • 17
  • 5
  • 3
    Possible duplicate of [Convert a bitmap into a byte array](http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array) – Strider Feb 17 '16 at 15:57
  • Possible duplicate of [converting Java bitmap to byte array](http://stackoverflow.com/questions/4989182/converting-java-bitmap-to-byte-array) – Douglas Nassif Roma Junior Feb 17 '16 at 17:18

2 Answers2

0

Try something like this:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
malrok44
  • 592
  • 6
  • 17
0

Check the following answer: https://stackoverflow.com/a/4989543/3623735

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Community
  • 1
  • 1
user3623735
  • 345
  • 2
  • 9