0
fun Cocktails(cocktailImage: String)

var androidCocktail = ImageBitmap.imageResource(R.drawable.cocktailImage) 
//want's a ID not a String ! ;) So it's doesn't accept this way of writing.

Can i do something like var androidCocktail = ImageBitmap.imageResource(R.drawable. + cocktailImage) ? Or how does this work ?

Thanks in advance !

  • Are you trying to get a bitmap from a drawable resource? Are these images imported as drawables or are you trying load an image from a path on the device? – Banane42 Jan 30 '23 at 22:04
  • Where is the string coming from and what are you trying to load? The `imageResource` function can only load image **resources** which all have an `R.drawable.whatever` that you can use to get them. If you don't have an image resource, this function will be useless to you. – Tenfour04 Jan 30 '23 at 22:05
  • i did import 2 images as drawable, and want call my function and just give the name of my 1 of the 2 images. and then let the function to the rest. So i want include in my function R.drawable. and then just add the name of the image in my argument. – Kerbal Space Program Germany Jan 30 '23 at 22:07
  • Try changing the type of `cocktailImage: String` to `cocktailImage: Int` then pass `cocktailImage` into the `ImageBitmap.imageResource(cocktailImage)`. Then call your function like this. `Cocktails(R.drawable.cocktailImage)` – Banane42 Jan 30 '23 at 22:10
  • If you already have them loaded as Drawables, why would you pass String instead of Drawable to this function? There would be no way to retrieve your Drawable instance from the String. There's no relation. But anyway, Compose doesn't work with drawables. It can work with image resources, or you can directly pass an ImageBitmap to an Image. – Tenfour04 Jan 30 '23 at 22:16
  • yea i know this would work. but i wanted this different so that the user of my function doesn't have to know that R.drawable has to be written. So that every user can easy use my function without knowing to much about my function. my plan was calling Cockails("CocktailImage) And use this like this: fun Cocktails(cocktailImage: String) var androidCocktail = ImageBitmap.imageResource(R.drawable + cocktailImage) – Kerbal Space Program Germany Jan 30 '23 at 22:16
  • So the Strings that are passed in exactly match the names of image files in your `res/drawable` directories? – Tenfour04 Jan 30 '23 at 22:18

0 Answers0