活动结果启动器 Jetpack 中的多种 MIME 类型

问题描述 投票:0回答:3

我们如何在jetpack compose的活动结果启动器中设置多个mime类型。

val documentPickerLauncher = rememberLauncherForActivityResult(contract = contracts, 
onResult = {
          // performing operations with selected file.
        })
        val scope = rememberCoroutineScope()
        scope.launch {
        // launching the file picker
          launcher.launch("*/*")
      }

Here i can select all files in the storage. Instead of all files, i need to restrict this into png and pdf. How to acheive this?
android kotlin android-jetpack-compose android-jetpack
3个回答
0
投票

您需要将 MIME 类型添加到意图中:

intent.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/png", "application/pdf"))

我不能 100% 确定这些 MIME 类型是否正确


0
投票
val documentPickerLauncher = rememberLauncherForActivityResult(
    contract = contracts,
    onResult = {
        // performing operations with selected file.
    })
val scope = rememberCoroutineScope()
scope.launch {
    // launching the file picker
    launcher.launch(arrayOf("application/pdf", "image/png"))
}

0
投票

您可以通过重写

createIntent
函数来做到这一点。这段代码可以做到这一点:

val documentPickerLauncher = rememberLauncherForActivityResult(
    contract = object : ActivityResultContracts.GetContent() {
            override fun createIntent(context: Context, input: String): Intent {
                return super.createIntent(context, input)
                    .putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/png", "application/pdf"))
            }
        }, 
    onResult = {
          // performing operations with selected file.
        }
)

val scope = rememberCoroutineScope()
scope.launch {
        // launching the file picker
          launcher.launch("*/*")
      }
© www.soinside.com 2019 - 2024. All rights reserved.