Image
Access Image from assets/
Section titled “Access Image from assets/”val context = LocalContext.currentval bitmap = remember { val file = context.assets.open(<filePath>) BitmapFactory.decodeStream(file)}
Image( bitmap = bitmap.asImageBitmap(), contentDescription = <filePath>)Network Image (Raw)
Section titled “Network Image (Raw)”@Composablefun NetworkImage() { var bitmap by remember { mutableStateOf<Bitmap?>(null) } LaunchedEffect(Unit) { withContext(Dispatchers.IO) { try { bitmap = URL("https://skills-music-api.eliaschen.dev/image/ocean.jpg").openStream() .use { BitmapFactory.decodeStream(it) } ?: null } catch (e: Exception) { bitmap = null } } } Column { bitmap?.asImageBitmap()?.let { Image(bitmap = it, contentDescription = "") } }}Network Image (OkHttp)
Section titled “Network Image (OkHttp)”@Composablefun NetworkImage(url: String) { val bitmap = remember { mutableStateOf<android.graphics.Bitmap?>(null) }
LaunchedEffect(url) { bitmap.value = withContext(Dispatchers.IO) { OkHttpClient() .newCall(Request.Builder().url(url).build()) .execute() .use { response -> response.body?.bytes()?.let { android.graphics.BitmapFactory.decodeByteArray(it, 0, it.size) } } } }
bitmap.value?.let { Image(bitmap = it.asImageBitmap(), contentDescription = "Network image") }}