5

How to convert imageview to bytearray kotlin android

In java

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();

return image
Best Best
  • 515
  • 2
  • 8
  • 17
  • 2
    You can copy paste this code in your Kotlin file in Android Studio and it will get converted to Kotlin. – zsmb13 Oct 10 '17 at 12:05

2 Answers2

23

Here it is use java to kotlin converter.

val bitmap = (image.getDrawable() as BitmapDrawable).getBitmap()
val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream)
val image = stream.toByteArray()
Muzammil Husnain
  • 1,218
  • 1
  • 10
  • 24
3

This may help you,

private fun imageToBitmap(image: ImageView): ByteArray {
    val bitmap = (image.drawable as BitmapDrawable).bitmap
    val stream = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream)

    return stream.toByteArray()
}
UltimateDevil
  • 2,807
  • 2
  • 18
  • 31