Home > OS >  Finding import and dependency with Android Studio
Finding import and dependency with Android Studio

Time:05-11

I am trying to use imageResource in an Android Jetpack Compose app but I cannot find the depndency to add to my grade file.

imageResource is in the docs here:

androidx.compose.ui.res

Image(
    bitmap = imageResource(id = R.drawable.background),
    modifier = Modifier.fillMaxSize(),
    contentScale = ContentScale.Crop
)

When I add androidx.compose.ui.res to my grade file it fails to resolve.

How do I find the dependency to add to my grade file for immageresouzrce?

CodePudding user response:

No dependency, the link you posted clearly states the method is accessible though ImageBitmap.Companion.imageResource(...), so that is what it is.

Type the exact phrase and you'll be good.

Explicitly using the Companion property is not necessary, you could directly use ImageBitmap.imageResource(...) here as well.

CodePudding user response:

imageResource is declared on ImageBitmap companion, so you have to call it like this:

Image(
    bitmap = ImageBitmap.imageResource(R.drawable.background),
    //...
)

Also in the provided code bitmap doesn't seems to be really needed, so consider using painterResource instead:

Image(
    painter = painterResource(R.drawable.background),
    //...
)
  • Related