如何使用Jetpack Compose从图库中选择多张照片?

huangapple go评论76阅读模式
英文:

How to select multiple photos from gallery using jetpack compose?

问题

@Composable
fun ItemScreen(){
  Column(modifier = Modifier
    .fillMaxSize()
    .verticalScroll(rememberScrollState())
    .padding(10.dp)) {
      Headline(modifier = Modifier.fillMaxWidth())
      AddItemRow(titleName = "输入颜色", 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 = "选择照片", 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))
        }
    }
 }
}
英文:

In my project i want to display the paths of gallery photos using lazy column in jetpack compose but when i select any photos and return back to screen , lazy column dost not show any paths. This my code

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

 @Composable
fun OpenGallery(){
  var selectedImage =  remember { mutableStateListOf&lt;Uri?&gt;() }
  val launcher = rememberLauncherForActivityResult(contract = ActivityResultContracts.GetMultipleContents()){
    selectedImage = it.toMutableStateList()
  }
  GalleryContent(selectedImage) {
    launcher.launch(&quot;image/jpeg&quot;)
  }
}

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

How do i fix it . Please help

答案1

得分: 2

你可以使用以下代码代替 selectedImage = it.toMutableStateList()

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

Instead of selectedImage = it.toMutableStateList() you can use:

val selectedImage = remember { mutableStateListOf&lt;Uri?&gt;(null) }
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) {
    selectedImage.apply {
        clear()
        addAll(it)
    }
}

huangapple
  • 本文由 发表于 2023年2月14日 01:58:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定