简体   繁体   中英

How to convert byte array to image [kotlin]

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
    }

Try with this:

 fun byteArrayToBitmap(data: ByteArray): Bitmap {
    return BitmapFactory.decodeByteArray(data, 0, data.size)
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM