android 如何选择多张照片从画廊使用jetpack组成?

mklgxw1f  于 2023-02-14  发布在  Android
关注(0)|答案(1)|浏览(163)

在我的项目中,我想显示的路径画廊照片使用惰性列在jetpack组成,但当我选择任何照片,并返回到屏幕,惰性列不显示任何路径。这是我的代码

@Composable
fun ItemScreen(){
  Column(modifier = Modifier
    .fillMaxSize()
    .verticalScroll(rememberScrollState())
    .padding(10.dp)) {
      Headline(modifier = Modifier.fillMaxWidth())
      AddItemRow(titleName = "Enter Color", KeyboardOptions(keyboardType = KeyboardType.Text))
      OpenGallery()
}

 @Composable
fun OpenGallery(){
  var selectedImage =  remember { mutableStateListOf<Uri?>() }
  val launcher = rememberLauncherForActivityResult(contract = ActivityResultContracts.GetMultipleContents()){
    selectedImage = it.toMutableStateList()
  }
  GalleryContent(selectedImage) {
    launcher.launch("image/jpeg")
  }
}

 @Composable
fun GalleryContent(
selectedImage: MutableList<Uri?> ?= null,
OnImageClick: () -> Unit
)
{
 if (selectedImage?.isEmpty() == true) {
    Button(
        onClick = OnImageClick,
    ) {
        Text(text = "Choose Photos", color = Color.White, style = MaterialTheme.typography.bodySmall)
    }
}
 else{
    LazyColumn(modifier = Modifier.height(300.dp)) {
        items(selectedImage!!){ it ->
            Text(text = it?.path.toString(), color = Color.Black)
            Spacer(modifier = Modifier.height(9.dp))
        }
    }
 }
}

我该怎么修,请帮帮忙

jslywgbw

jslywgbw1#

您可以使用以下命令代替selectedImage = it.toMutableStateList()

val selectedImage = remember { mutableStateListOf<Uri?>(null) }
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) {
    selectedImage.apply {
        clear()
        addAll(it)
    }
}

相关问题