Answer the question
In order to leave comments, you need to log in
How to set image from MainActivity to Image Jetpack Compose?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GlobalScope.launch(Dispatchers.IO) {
val url = URL("https://picsum.photos/300/300")
val connection = url.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
val inputStream = connection.inputStream
val bitmap = BitmapFactory.decodeStream(inputStream)
launch(Dispatchers.Main) {
setContent {
PlayGroundTheme {
Surface(color = MaterialTheme.colors.background) {
Greeting("Android", bitmap)
}
}
}
}
}
}
}
@Composable
fun Greeting(name: String, bitmap: Bitmap) {
Column(Modifier.fillMaxWidth().padding(16.dp)) {
Image(bitmap.asImageBitmap(), Modifier
.preferredHeight(300.dp)
.fillMaxWidth() )
Text(text = "Hello $name")
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question