3

I want to convert byte array to image and show it in image view, but not sure how to do it. Can someone guide me please

I use this function to convert bitmap to byte array

private fun BitmapToByteArray(): ByteArray
    {
        val stream = ByteArrayOutputStream()
        btm1!!.compress(Bitmap.CompressFormat.PNG, 100, stream)
        val bitmapdata: ByteArray = stream.toByteArray()
        return bitmapdata
    }

2 Answers2

1

These extension functions are working

fun Bitmap.toByteArray(): ByteArray {
  val stream = ByteArrayOutputStream()
  this.compress(Bitmap.CompressFormat.PNG, 100, stream)
  return stream.toByteArray()
}

fun ByteArray.toBitmap(): Bitmap {
  return BitmapFactory.decodeByteArray(this, 0, this.size)
}
Ahmet B.
  • 1,290
  • 10
  • 20
0

Try with this :

 fun byteArrayToBitmap(data: ByteArray): Bitmap {
    return BitmapFactory.decodeByteArray(data, 0, data.size)
}
Shay Kin
  • 2,539
  • 3
  • 15
  • 22