Skip to content Skip to sidebar Skip to footer

Convert To And From Uncompressed Bitmap To Byte Array

I am trying to implement two methods. One that takes an ImageView as input and outputs uncompressed byte array. The second takes byte array input and converts to a bitmap. These a

Solution 1:

Try this on your imageToBytes method

ByteArrayOutputStreamstream=newByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

And on your bytesToImage

BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);

Solution 2:

So the problem is with the bitmap in bytesToImage being incorrectly configured.

This requires the original bitmap to be passed in directly.

Here is the updated bytesToImage method which gives correct answer.

privatestatic Bitmap bytesToImage(byte data[], Bitmap originalImage) {

    Bitmap newBmp;
    newBmp = Bitmap.createBitmap(originalImage.getWidth(), originalImage.getHeight(), originalImage.getConfig());


    ByteBufferbuffer1= ByteBuffer.wrap(data);

    buffer1.rewind();
    newBmp.copyPixelsFromBuffer(buffer1);

    byte[] imageInByte = null;



    ByteBufferbyte_buffer= ByteBuffer.wrap(data);



    byte_buffer.rewind();



    newBmp.copyPixelsFromBuffer(byte_buffer);


    return newBmp;
}

Post a Comment for "Convert To And From Uncompressed Bitmap To Byte Array"