I would like to let user add image to each item (Card) in LazyColumn. But it seems that images are getting deleted on recomposition. How can I fix that?
@Composable
fun PhotoUpload(
) {
val imageUri = remember {
mutableStateOf<Uri?>(null)
}
val context = LocalContext.current
val bitmap = remember {
mutableStateOf<Bitmap?>(null)
}
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.GetContent()
) { uri: Uri? ->
imageUri.value = uri
}
imageUri.value?.let {
LaunchedEffect(Unit) {
if (Build.VERSION.SDK_INT < 28) {
bitmap.value = MediaStore.Images
.Media.getBitmap(context.contentResolver, it)
} else {
val source = ImageDecoder
.createSource(context.contentResolver, it)
bitmap.value = ImageDecoder.decodeBitmap(source)
}
}
}
bitmap.value?.let { btm ->
Image(
bitmap = btm.asImageBitmap(),
contentDescription = null,
modifier = Modifier.size(400.dp)
)
}
Button(onClick = {
launcher.launch("image/*")
}) {
Icon(Icons.Filled.PhotoAlbum, "")
}
}
Images get deleted (they're not coming back, it's a loop gif)
PS: For LazyColumn I do use keys. And I also tried using AsyncImage from Coil, but it had the same issue
you only trigger composition for only one item. You can check out this answer as well https://stackoverflow.com/a/74700668/5457853
– Thracian Dec 06 '22 at 15:21