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")
}
}
так работает, но хотелось бы отдельно на image повесить загрузку, через xml получается, при применении coroutines через compose пишет:
@Composable invocations can only happen from the context of a @Composable function,
как получить доступ отдельно к image в MainActivity?