I have a `ByteArray` that contains the binary data...
# multiplatform
v
I have a
ByteArray
that contains the binary data of an image (image/png), and I need to show it. Can I somehow convert it into a Bitmap? Like In Android, it was done by
BitmapFactory.decodeByteArray
...
l
Yes, you can do this using an expect/actual implementation
m
I did it without expect/actual actually. @Vram Voskanyan check my comment here: https://kotlinlang.slack.com/archives/C0346LWVBJ4/p1713089530627749?thread_ts=1711662097.909009&channel=C0346LWVBJ4&message_ts=1713089530.627749 If it’s not enough information i can send you more code, got it to work last week.
m
In common code:
Copy code
expect fun ByteArray.decodeToImageBitmap(): ImageBitmap?
On Android:
Copy code
actual fun ByteArray.decodeToImageBitmap(): ImageBitmap? {
    return BitmapFactory.decodeByteArray(this, 0, this.size).asImageBitmap()
}
On Desktop:
Copy code
actual fun ByteArray.decodeToImageBitmap(): ImageBitmap? {
    return Image.makeFromEncoded(this).toComposeImageBitmap()
}
On iOS: Same as for desktop.
v
Thanks, it works as expected. 🙌