英文:
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 = "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))
}
}
}
}
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<Uri?>(null) }
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) {
selectedImage.apply {
clear()
addAll(it)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论