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

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

How to select multiple photos from gallery using jetpack compose?

问题

  1. @Composable
  2. fun ItemScreen(){
  3. Column(modifier = Modifier
  4. .fillMaxSize()
  5. .verticalScroll(rememberScrollState())
  6. .padding(10.dp)) {
  7. Headline(modifier = Modifier.fillMaxWidth())
  8. AddItemRow(titleName = "输入颜色", KeyboardOptions(keyboardType = KeyboardType.Text))
  9. OpenGallery()
  10. }
  11. @Composable
  12. fun OpenGallery(){
  13. var selectedImage = remember { mutableStateListOf<Uri?>() }
  14. val launcher = rememberLauncherForActivityResult(contract = ActivityResultContracts.GetMultipleContents()){
  15. selectedImage = it.toMutableStateList()
  16. }
  17. GalleryContent(selectedImage) {
  18. launcher.launch("image/jpeg")
  19. }
  20. }
  21. @Composable
  22. fun GalleryContent(
  23. selectedImage: MutableList<Uri?> ?= null,
  24. OnImageClick: () -> Unit
  25. )
  26. {
  27. if (selectedImage?.isEmpty() == true) {
  28. Button(
  29. onClick = OnImageClick,
  30. ) {
  31. Text(text = "选择照片", color = Color.White, style = MaterialTheme.typography.bodySmall)
  32. }
  33. }
  34. else{
  35. LazyColumn(modifier = Modifier.height(300.dp)) {
  36. items(selectedImage!!){ it ->
  37. Text(text = it?.path.toString(), color = Color.Black)
  38. Spacer(modifier = Modifier.height(9.dp))
  39. }
  40. }
  41. }
  42. }
英文:

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

  1. @Composable
  2. fun ItemScreen(){
  3. Column(modifier = Modifier
  4. .fillMaxSize()
  5. .verticalScroll(rememberScrollState())
  6. .padding(10.dp)) {
  7. Headline(modifier = Modifier.fillMaxWidth())
  8. AddItemRow(titleName = &quot;Enter Color&quot;, KeyboardOptions(keyboardType = KeyboardType.Text))
  9. OpenGallery()
  10. }
  11. @Composable
  12. fun OpenGallery(){
  13. var selectedImage = remember { mutableStateListOf&lt;Uri?&gt;() }
  14. val launcher = rememberLauncherForActivityResult(contract = ActivityResultContracts.GetMultipleContents()){
  15. selectedImage = it.toMutableStateList()
  16. }
  17. GalleryContent(selectedImage) {
  18. launcher.launch(&quot;image/jpeg&quot;)
  19. }
  20. }
  21. @Composable
  22. fun GalleryContent(
  23. selectedImage: MutableList&lt;Uri?&gt; ?= null,
  24. OnImageClick: () -&gt; Unit
  25. )
  26. {
  27. if (selectedImage?.isEmpty() == true) {
  28. Button(
  29. onClick = OnImageClick,
  30. ) {
  31. Text(text = &quot;Choose Photos&quot;, color = Color.White, style = MaterialTheme.typography.bodySmall)
  32. }
  33. }
  34. else{
  35. LazyColumn(modifier = Modifier.height(300.dp)) {
  36. items(selectedImage!!){ it -&gt;
  37. Text(text = it?.path.toString(), color = Color.Black)
  38. Spacer(modifier = Modifier.height(9.dp))
  39. }
  40. }
  41. }
  42. }

How do i fix it . Please help

答案1

得分: 2

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

  1. val selectedImage = remember { mutableStateListOf<Uri?>(null) }
  2. val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) {
  3. selectedImage.apply {
  4. clear()
  5. addAll(it)
  6. }
  7. }
英文:

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

  1. val selectedImage = remember { mutableStateListOf&lt;Uri?&gt;(null) }
  2. val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) {
  3. selectedImage.apply {
  4. clear()
  5. addAll(it)
  6. }
  7. }

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:

确定