Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

+imageResource from R.drawable causes IllegalArgumentException in Jetpack Compose

I was testing out Jetpack Compose with a new empty JC activity called HomeActivity. I am trying to load a drawable resource into my code. This is my function:

@Composable
fun home() {
    MaterialTheme(
        colors = colors,
        typography = typography
    ) {
        val menuIcon = +imageResource(R.drawable.ic_baseline_menu_24)
        val bottomActions = listOf<Image>()
        var (drawerState, onDrawerStateChangeListener) = +state { DrawerState.Closed }

        BottomDrawerLayout(drawerState = drawerState, onStateChange = onDrawerStateChangeListener, drawerContent = {
            Text(text = "Drawer")
        }) {
            Column(
                crossAxisSize = LayoutSize.Expand
            ) {
                BottomAppBar(
                    navigationIcon = {
                        drawerState = DrawerState.Opened
                        AppBarIcon(icon = menuIcon, onClick = {})
                    },
                    actionData = bottomActions,
                    action = {
                        AppBarIcon(icon = it, onClick = {})
                    }
                )
            }
        }
    }
}

According to the JC website, +imageResource(R.drawable.xyz) should load the resource as andX.ui Image object imageResource function returns Effect<Image>, it doesn't crash the app but I don't know how to resolve it to Image object needed for AppBarIcon

Some of the Logcat outputs:


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.craftmyspace/com.craftmyspace.activities.HomeActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter bitmap
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        ...os.ZygoteInit.main(ZygoteInit.java:858)


     Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter bitmap
        at androidx.ui.graphics.AndroidImage.<init>(Unknown Source:2)
        at androidx.ui.graphics.AndroidImageKt.imageFromResource(AndroidImage.kt:29)
        at androidx.ui.res.ImageResourcesKt$imageResource$1$1.invoke(ImageResources.kt:45)
        at androidx.ui.res.ImageResourcesKt$imageResource$1$1.invoke(Unknown Source:0)
        at androidx.compose.ComposerKt.remember(Composer.kt:1867)
        at androidx.compose.EffectsKt$memo$2.invoke(Effects.kt:287)
        at androidx.compose.EffectsKt$memo$2.invoke(Unknown Source:2)
        at androidx.compose.Effect.resolve(Effects.kt:106)
        at androidx.compose.Effect.resolve$default(Effects.kt:843)
        at androidx.compose.Effect.unaryPlus(Effects.kt:115)
        at androidx.ui.res.ImageResourcesKt$imageResource$1.invoke(ImageResources.kt:45)
        at androidx.ui.res.ImageResourcesKt$imageResource$1.invoke(Unknown Source:2)
        at androidx.compose.Effect.resolve(Effects.kt:106)
        at androidx.compose.Effect.resolve$default(Effects.kt:843)
        at androidx.compose.EffectsKt.unaryPlus(Effects.kt:841)
        at com.craftmyspace.activities.HomeActivityKt$home$1$1$1.invoke(HomeActivity.kt:66)

HomeActivity.kt:66 is this val menuIcon = +imageResource(R.drawable.ic_baseline_menu_24)

like image 526
Yahya Ayash Luqman Avatar asked Nov 08 '19 10:11

Yahya Ayash Luqman


People also ask

What is mutableStateOf in jetpack compose?

mutableStateOf creates an observable MutableState<T> , which is an observable type integrated with the compose runtime. interface MutableState<T> : State<T> { override var value: T. } Any changes to value will schedule recomposition of any composable functions that read value .

Is jetpack compose reactive?

Jetpack Compose is a modern toolkit designed to simplify UI development. It combines a reactive programming model with the conciseness and ease of use of the Kotlin programming language. It is fully declarative, meaning you describe your UI by calling a series of functions that transform data into a UI hierarchy.


3 Answers

UPDATE: Compose_version = '1.0.0-beta01'

val painter = painterResource(id = R.drawable.img)
Image(
       painter = painter,
       contentDescription = "Profile pic",
       modifier = imageModifier,
       contentScale = ContentScale.Crop
  )

UPDATE: 02/09/2020

In compose version 1.0.0-alpha01 you can use vectorResource(id = R.drawable.my_vector_resource_id) to load vector resources.

Ex: Image(asset = vectorResource(id = R.drawable.my_vector_resource_id)) or Icon(asset = vectorResource(id = R.drawable.my_vector_resource_id))

Old Answer

I have faced the same issue and observed that imageFromResource() and imageResource() can't load vector resources. You need to create a effectOf<VectorAsset> by providing the resource id and use DrawVector() to draw vector assets. You can use below @Composable function to draw vector resources.

@Composable
fun VectorImage(@DrawableRes id: Int, tint: Color = Color.Transparent) {
    val vector = +vectorResource(id)
    WithDensity {
        Container(
            width = vector.defaultWidth.toDp(),
            height = vector.defaultHeight.toDp()
        ) {
            DrawVector(vectorImage = vector, tintColor = tint)
        }
    }
}
like image 97
islamdidarmd Avatar answered Sep 27 '22 20:09

islamdidarmd


Can be made easier

   Image(
    vectorResource(R.drawable.folk),
    contentScale = ContentScale.Crop
)
like image 41
bigmeco Avatar answered Sep 27 '22 20:09

bigmeco


In my case i solve removing ic_chat_bubble.xml(anydpi) from drawable ic_chat_bubble resource now it's work fine.

enter image description here

like image 23
Anas Mehar Avatar answered Sep 27 '22 21:09

Anas Mehar