Skip to content

Instantly share code, notes, and snippets.

@vvkirillov
Created October 21, 2015 12:53
Show Gist options
  • Save vvkirillov/6e0475a56b9b2b14cd97 to your computer and use it in GitHub Desktop.
Save vvkirillov/6e0475a56b9b2b14cd97 to your computer and use it in GitHub Desktop.
Android bitmap conversion to and from byte array
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public final class BitmapUtils {
private BitmapUtils(){}
/**
* Converts bitmap to byte array in PNG format
* @param bitmap source bitmap
* @return result byte array
*/
public static byte[] convertBitmapToByteArray(Bitmap bitmap){
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}finally {
if(baos != null){
try {
baos.close();
} catch (IOException e) {
Log.e(BitmapUtils.class.getSimpleName(), "ByteArrayOutputStream was not closed");
}
}
}
}
/**
* Converts bitmap to the byte array without compression
* @param bitmap source bitmap
* @return result byte array
*/
public static byte[] convertBitmapToByteArrayUncompressed(Bitmap bitmap){
ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(byteBuffer);
byteBuffer.rewind();
return byteBuffer.array();
}
/**
* Converts compressed byte array to bitmap
* @param src source array
* @return result bitmap
*/
public static Bitmap convertCompressedByteArrayToBitmap(byte[] src){
return BitmapFactory.decodeByteArray(src, 0, src.length);
}
}
@hudsonpereira
Copy link

this gist saved me, thanks

@kibotu
Copy link

kibotu commented Feb 21, 2020

here the kotlin equivalent for convertBitmapToByteArrayUncompressed

fun Bitmap.convertToByteArray(): ByteArray = ByteBuffer.allocate(byteCount).apply {
    copyPixelsToBuffer(this)
    rewind()
}.array()

@Novikov
Copy link

Novikov commented Mar 16, 2022

Second solution doesn't works for me.

@facext81
Copy link

facext81 commented Sep 9, 2022

fun Bitmap.convertToByteArray(): ByteArray = ByteArrayOutputStream().apply {
compress(Bitmap.CompressFormat.JPEG, 100, this)
}.toByteArray()

@Lilimester
Copy link

Lilimester commented Feb 20, 2024

 /**
     * Converts bitmap to the byte array without compression
     * @param bitmap source bitmap
     * @return result byte array
     */
    public static byte[] convertBitmapToByteArrayUncompressed(Bitmap bitmap){
        ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(byteBuffer);
        byteBuffer.rewind();
        return byteBuffer.array();
    }

Is there a way to convert the array obtained without compress to bitmap. as in majority of case, it is returning null ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment