4

I would like to get a byte array from an jpeg image located in my res/drawable file ?

Does anyone know how to do that please ?

Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
Spredzy
  • 4,982
  • 13
  • 53
  • 69
  • I believe you can use solution at http://stackoverflow.com/questions/6602417/get-the-uri-of-an-image-stored-in-drawable/36062748#36062748 – Boris Treukhov Aug 20 '16 at 20:29

3 Answers3

11
    Drawable drawable;

    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] bitmapdata = stream.toByteArray();
David
  • 37,109
  • 32
  • 120
  • 141
Sandy
  • 428
  • 1
  • 5
  • 16
7

Get a bitmap decodeResource(android.content.res.Resources, int) Then either compress it to ByteArrayOutputStream() or copyPixelsToBuffer and get your array from the buffer. http://developer.android.com/reference/android/graphics/Bitmap.html

Alex Volovoy
  • 67,778
  • 13
  • 73
  • 54
2
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mPhoto.compress(Bitmap.CompressFormat.JPEG /* FileType */,
                        100 /* Ratio */, stream);

HTH !

Karan
  • 12,724
  • 6
  • 40
  • 33