android - 如何在撰写中使用意图启动文件选择器
问题描述
我正在尝试在单击按钮时启动文件选择器(可组合功能)。无法使用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
)
推荐阅读
- excel - 我写的 Excel 函数显示#value
- sql - 如何在多列上进行透视
- sql - 如何在 SQL Server 中的两个表(表顺序和表产品)的相同字段上添加关系?
- mysql - 如何使用 IN() 子句通过多个父 ID 获取所有子代
- php - htaccess 用于 seo 友好的 url 和 php 会话稳定性
- php - 如何传递数组中的变量名
- material-ui - MUI Grid 组件的自定义间距
- javascript - 在组件状态中附加 JSON 对象并显示它
- azure - 如何通过 MQTT 为 Azure IoT Hub 消息设置超时?
- python-3.x - 用python清晰地显示数据框