Home > Tricks & Tips > Convert Bitmap to byte array and reverse

Convert Bitmap to byte array and reverse


If you want to convert a `Bitmap` to byte array:

public byte[] getByteArray(Bitmap bitmap) {
	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	bitmap.compress(CompressFormat.PNG, 0, bos);
	return bos.toByteArray();
}

and if you want the reverse process:

public Bitmap getBitmap(byte[] bitmap) {
	return BitmapFactory.decodeByteArray(bitmap , 0, bitmap.length);
}

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , , ,
  1. Yusuf
    February 14, 2016 at 5:35 am

    Merci beaucoup le gar, thx.

  2. jagdish
    September 13, 2014 at 11:46 am

    I want to send image from android to matlab. In this i firstly converted that image into bitmap and convert into bytearray.i not know how to send byte array or can i send pixels array.plz help

  3. Umresh
    September 2, 2014 at 5:35 pm

    hey i need to convert a bytebuffer to bitmap.here is code

    byte[] bytes = new byte[10];
    byte_buffer = ByteBuffer.wrap(bytes);
    bytes = new byte[byte_buffer.remaining()];
    byte_buffer.get(bytes, 0, bytes.length);
    byte_buffer.clear();
    bytes = new byte[byte_buffer.capacity()];
    byte_buffer.get(bytes, 0, bytes.length);
    Bitmap sb = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    Error log:
    — SkImageDecode : Factory returned null;

  4. June 27, 2014 at 9:25 am

    This is really interesting, You’re a very skilled blogger.
    I’ve joined your feed and look forward to seeking more of
    your wonderful post. Also, I have shared your web site in my social networks!

  5. June 18, 2014 at 7:57 am

    Hi there, I discovered your site by the use of Google
    even as searching for a similar topic, your website came up, it seems
    good. I have bookmarked it in my google bookmarks.

    Hi there, simply changed into alert to your blog
    thru Google, and found that it’s really informative.
    I am going to be careful for brussels. I’ll be grateful for those who continue this in future.
    A lot of folks might be benefited from your writing. Cheers!

  6. May 23, 2014 at 5:05 am

    Very good post. I absolutely appreciate this
    site. Continue the good work!

  7. shwetahuddar
    May 23, 2013 at 1:07 pm

    I converted image into grayscale then read the pixels like this
    for (int i = 0; i < image.getHeight(); i++)
    {
    for (int j = 0; j < image.getWidth(); j++)
    {
    pixels[k]=(byte) (image.getPixel(j, i));
    pixels2[k]=unsignedToBytes(pixels[k]);
    k++;
    }
    }
    And performed edge detection now getting positive byte array so how should i again display it as image.

    tried this one but not getting result

    imageView1.setImageBitmap(BitmapFactory.decodeByteArray(edgeImage1, 0, edgeImage1.length));

    please help us…..

  8. sameer
    March 29, 2013 at 12:49 am

    I have byte array data of a jpg image. Is it possible to convert the array into the bitmap.

  9. January 10, 2013 at 9:09 pm

    I propose this method:
    private byte[] Convert(Bitmap bmp) {
    int bytes = bmp.getWidth() * bmp.getHeight() * 4;
    Log.e(“Bytes”, String.valueOf(bytes));
    ByteBuffer buffer = ByteBuffer.allocate(bytes);
    bmp.copyPixelsToBuffer(buffer);
    byte[] array = buffer.array();
    if (array != null) {
    return array;
    }
    return null;
    }

  10. AdeelTariq
    September 10, 2012 at 9:06 pm

    I am getting “SkImageDecoder::Factory returned null “, do u have any idea why it’s coming ,

    • Gabriel Vieira
      March 22, 2013 at 7:40 pm

      your bitmap is null

  11. I-Valiwtis
    June 1, 2012 at 7:52 am

    Thanx man.Works like a charm!

  12. pavanrekha laharpure
    May 11, 2012 at 12:03 am

    shubhampatni86 :

    I am getting “SkImageDecoder::Factory returned null “, do u have any idea why it’s coming ,
    as I am getting size of byte[] as well.

  13. May 7, 2012 at 4:07 pm

    I am getting “SkImageDecoder::Factory returned null “, do u have any idea why it’s coming ,
    as I am getting size of byte[] as well.

  1. No trackbacks yet.

Leave a comment