ANDROID-KOTLIN App crashing before starting

Viewed 32

I am trying to make the tutorials from a Jetpack Compose book but the app crashes when I start it:

This is the error code

2022-09-15 22:03:05.983 14360-14360/com.raywenderlich.android.jetreddit E/AndroidRuntime: FATAL EXCEPTION: main Process: com.raywenderlich.android.jetreddit, PID: 14360 java.lang.IllegalArgumentException: Only VectorDrawables and rasterized asset types are supported ex. PNG, JPG at androidx.compose.ui.res.PainterResources_androidKt.loadImageBitmapResource(PainterResources.android.kt:99) at androidx.compose.ui.res.PainterResources_androidKt.painterResource(PainterResources.android.kt:71) at com.raywenderlich.android.jetreddit.components.PostKt.ImageContent(Post.kt:196) at com.raywenderlich.android.jetreddit.components.PostKt$ImagePost$1.invoke(Post.kt:74) at com.raywenderlich.android.jetreddit.components.PostKt$ImagePost$1.invoke(Post.kt:73) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)

That's the position from the error code:

@Composable
fun ImageContent(image: Int) {
    val imageAsset = ImageBitmap.imageResource(id = image)
    Image(
        bitmap = imageAsset,
        contentDescription = stringResource(id = R.string.post_header_description),
        modifier = Modifier
            .fillMaxWidth()
            .aspectRatio(imageAsset.width.toFloat() / imageAsset.height),
        contentScale = ContentScale.Crop
    )
}

@Composable
fun ImagePost(post: PostModel) {
    Post(post) {
        ImageContent(post.image ?: R.drawable.compose_course)
    }
}
1 Answers

From the exception, It seems you are trying to load an image that is not supported by ImageBitmap.imageResource.

java.lang.IllegalArgumentException: Only VectorDrawables and rasterized asset types are supported ex. PNG, JPG.

Check that if the type of post.image or R.drawable.compose_course is supported by the API you are using.

Related