Created
October 21, 2015 12:53
-
-
Save vvkirillov/6e0475a56b9b2b14cd97 to your computer and use it in GitHub Desktop.
Android bitmap conversion to and from byte array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
here the kotlin equivalent for convertBitmapToByteArrayUncompressed
fun Bitmap.convertToByteArray(): ByteArray = ByteBuffer.allocate(byteCount).apply {
copyPixelsToBuffer(this)
rewind()
}.array()
Second solution doesn't works for me.
fun Bitmap.convertToByteArray(): ByteArray = ByteArrayOutputStream().apply {
compress(Bitmap.CompressFormat.JPEG, 100, this)
}.toByteArray()
/**
* 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
this gist saved me, thanks