How to convert imageview to bytearray in kotlin

14,485

Solution 1

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()

Solution 2

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()
}
Share:
14,485

Related videos on Youtube

Best  Best
Author by

Best Best

Updated on June 04, 2022

Comments

  • Best  Best
    Best Best almost 2 years

    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
    
    • zsmb13
      zsmb13 over 6 years
      You can copy paste this code in your Kotlin file in Android Studio and it will get converted to Kotlin.
  • Shubham AgaRwal
    Shubham AgaRwal over 5 years
    Note: AFAIK PNG is lossless and quality parameter has no effect