我正在尝试在单击按钮时启动文件选择器(可组合功能)。无法使用 startActivityForResult()
.
@Composable
fun SelectScreen() {
Button(onClick = {
val intent = Intent(Intent.ACTION_GET_CONTENT)
startActivity(intent)
}
) {
Text("BUTTON")
}
}
最佳答案
这是我的建议:
val pickPictureLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.GetContent()
) { imageUri ->
if (imageUri != null) {
// Update the state with the Uri
}
}
// In your button's click
pickPictureLauncher.launch("image/*")
并在显示图像的可组合中,您可以执行以下操作val image = remember {
// Make sure to resize and compress
// the image to avoid display a big bitmap
ImageUtils.imageFromUri(imageUi)
}
Image(
image,
contentDescription = null
)
关于android - 如何在撰写中使用 Intent 启动文件选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68768236/