Hit*_*tel 8 android filechooser filepicker kotlin android-jetpack-compose
我试图通过单击按钮(可组合函数)来启动文件选择器。无法使用startActivityForResult()。
@Composable
fun SelectScreen() {
Button(onClick = {
val intent = Intent(Intent.ACTION_GET_CONTENT)
startActivity(intent)
}
) {
Text("BUTTON")
}
}
Run Code Online (Sandbox Code Playgroud)
ngl*_*ber 20
这是我的建议:
val pickPictureLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.GetContent()
) { imageUri ->
if (imageUri != null) {
// Update the state with the Uri
}
}
// In your button's click
pickPictureLauncher.launch("image/*")
Run Code Online (Sandbox Code Playgroud)
在显示图像的可组合项中,您可以执行以下操作
val image = remember {
// Make sure to resize and compress
// the image to avoid display a big bitmap
ImageUtils.imageFromUri(imageUi)
}
Image(
image,
contentDescription = null
)
Run Code Online (Sandbox Code Playgroud)
您可以使用rememberLauncherForActivityResult()注册请求来Activity#startActivityForResult指定给定的ActivityResultContract。
这将在与该调用者相关联的记录中创建一条记录ActivityResultRegistry,管理请求代码以及后台的转换Intent。
就像是:
val result = remember { mutableStateOf<Uri?>(null) }
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) {
result.value = it
}
Button(onClick = { launcher.launch(arrayOf("image/*")) }) {
Text(text = "Open Document")
}
result.value?.let {
//...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7329 次 |
| 最近记录: |