alfietap
alfietap

Reputation: 2279

Jetpack Compose not capturing rememberImagePainter in screenshot

I'm using the below code from this answer to draw my composable function to a canvas which in turn saves the image to the users phone which works fine, until I use rememberImagePainter in my Image composable and doesn't give me any error message when the app crashes.

val bounds = capturingViewBounds ?: return@clickable
bitmap = Bitmap.createBitmap(
    bounds.width.roundToInt(), bounds.height.roundToInt(),
    Bitmap.Config.ARGB_8888
).applyCanvas {
    translate(-bounds.left, -bounds.top)
    view.draw(this)
}

I'm loading a PNG image from the web directly into this Image composable based on an if statement. If I use a normal drawable from my folder on the image, there's no issue, it just seems to be when i load an image from the web it crashes

Image(
    modifier = Modifier
        .align(Alignment.CenterHorizontally)
        .size(48.dp),
    painter = if (player.playerImageUrl.isNotBlank()) {
        rememberImagePainter(data = player.playerImageUrl)
    } else {
        painterResource(
            id = setKitColour(kitColour)
        )
    },
    contentDescription = null
)
private fun setKitColour(color: String): Int {
    return when (color) {
        "Red" -> R.drawable.ic_shirt_red
        "Blue" -> R.drawable.ic_shirt_blue
        "Green" -> R.drawable.ic_shirt_green
        "Yellow" -> R.drawable.ic_shirt_yellow
        "White" -> R.drawable.ic_shirt_white
        else -> R.drawable.ic_shirt_black
    }
}

enter image description here

Upvotes: 2

Views: 3748

Answers (1)

Phil Dukhov
Phil Dukhov

Reputation: 88112

doesn't give me any error message when the app crashes

This is not the case, at least in my case I got the following exception:

java.lang.IllegalArgumentException: Software rendering doesn't support hardware bitmaps

Which is a known problem in Coil, and can be fixed by specifying allowHardware(false):

Coil 2.0.0 version:

Image(
    rememberAsyncImagePainter(
        ImageRequest.Builder(LocalContext.current)
            .data(data = imageURL)
            .allowHardware(false)
            .build()
    ),
    contentDescription = null
)

Coil 1.4.0 version:

Image(
    rememberImagePainter(
        data = player.playerImageUrl,
        builder = {
            allowHardware(false)
        }
    ),
    contentDescription = "..."
)

Upvotes: 2

Related Questions