I can’t pass a drawable id (R.drawable.image) as a...
# compose
a
I can’t pass a drawable id (R.drawable.image) as an INT for my painterResource(). When running in release mode I get the following crash:
java.lang.IllegalArgumentException: Only VectorDrawables and rasterized asset types are supported ex. PNG, JPG
the image I pass as an INT is a PNG.
Working:
Copy code
painterResource(id = R.drawable.cst_button1)
Not working:
Copy code
myImageId: Int = R.drawable.cst_button1
painterResource(id = myImageId)
a
don't explicitly type the data type .. and try .
Copy code
val imageId:Int = R.drawable.ic_launcher_foreground
val p = painterResource(id = imageId)
Icon(painter = p, contentDescription = "")
this one is working on my end . maybe you need to add your image through res-> new -> Image Asset .
a
Actually I am storing this myImageId in a Room database so I have to specify it. I just showcased it like this for simplicity but I just realized that it is also working on my end. Super weird, it just crashes when getting the ID from the data object stored in the Room database
a
its because the id actually memory address . and room database do not provide memory address .you maybe able to use it as bitmap .try storing bytearray to database and then get as bitmap and i think bitmap support all the places in android ,
e
not a compose issue. it is not safe to store a resource id in room, the value can change between builds
k
If you need to persist a resource as part of a model you can set up a dictionary of values that you control mapped to resource ids and store those values in your database instead. Be sure to handle the case of values being changed/removed if you do, though
951 Views