android - 如何在撰写中使用 Intent 启动文件选择器

标签 android kotlin android-jetpack-compose

我正在尝试在单击按钮时启动文件选择器(可组合功能)。无法使用 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/

相关文章:

android - 使用 OpenCV 在 Android 中将 4 channel 矩阵转换为 1 channel

java - 退出 Java/Kotlin 应用程序时阻止 bash 脚本死亡

flutter - Kotlin 中是否有类似于 Dart 的 Completer 行为的东西?

kotlin - 传递和访问已传递到需要其父类(super class)型的类构造方法中的子类型的值和函数

android - 线圈不使用 Jetpack Compose 在模拟器中加载图像

android - 将 JSON 转换为游标

android - 使用 getStream 的聊天和提要应用程序,具有自定义 UI

android - 代码未到达 if 语句

android - 在 jetpack compose 中对齐行项目

android - OutlinedTextField jetpack 在键盘后面组成