I have a Compose Multiplatform project generated u...
# compose
j
I have a Compose Multiplatform project generated using kmp.jetbrains.com. In the project I'm converting data in to a
ImageBitmap
using
androidx.compose.ui.graphics.toComposeImageBitmap
....this works on all platform except for Android for some reason? Any known reason why that wold be the case? The same compose code/dependencies are used on all platforms.
r
What data are you converting exactly?
I don't recall
toComposeImageBitmap
being a core Compose API
a
Can you attach code snippet you use for conversion? If it contains
org.jetbrains.skia
classes, it would not compile for android
r
Android has Bitmap.asComposeImageBitmap
j
ah, looks like there is skia dependencies
I'm using only Compose Multiplatform dependencies here btw
a
If so, you need to write expect function that uses Bitmap.asComposeImageBitmap on android and your code on other platforms
j
cool, thanks, I'l try that
a
You can take ImageViewer example as a reference
j
fwiw this is snippet of code I have (converting from base64 data)
Copy code
val decodedBytes = Base64.Default.decode(imageData)
image = Image.makeFromEncoded(decodedBytes).toComposeImageBitmap()

...

Image(
    painter = BitmapPainter(image),
    ....
)
hmm, I'm using the Compose Multiplatform dependencies for Android as well so don't think I have access to
Bitmap.asComposeImageBitmap
is there particular jetpack compose dependency I need to pull in for that?
a
Check the link above. It is exact what you need.
j
ah, thanks, I missed that
m
I use this function implementation on desktop (and all Skia based platforms)
Copy code
fun imageBitmapFromBytes(encodedImageData: ByteArray): ImageBitmap {
    return Image.makeFromEncoded(encodedImageData).toComposeImageBitmap()
}
and this on Android where we don’t have Skia access.
Copy code
fun imageBitmapFromBytes(encodedImageData: ByteArray): ImageBitmap {
    return BitmapFactory.decodeByteArray(encodedImageData, 0, encodedImageData.size).asImageBitmap()
}
The ByteArray is assumed to contain the bytes of a PNG or JPEG image.
👍 2