将多个 Mime 类型传递给 ActivityResultLauncher.launch()

Posted

技术标签:

【中文标题】将多个 Mime 类型传递给 ActivityResultLauncher.launch()【英文标题】:Passing Multiple Mime Types to ActivityResultLauncher.launch() 【发布时间】:2021-06-23 01:58:38 【问题描述】:

我有以下代码

val getContent = registerForActivityResult(ActivityResultContracts.GetContent())  uri: Uri? ->
    //Some code here..

还有其他地方,

getContent.launch("application/vnd.openxmlformats-officedocument.wordprocessingml.document")

我可以成功选择 docx 文件。我需要选择 pdf 或 doc 或 text 或 docx 而只是能够选择一种(此处为 docx)。

【问题讨论】:

【参考方案1】:

我建议使用OpenDocument 而不是GetContent

val documentPick =
    registerForActivityResult(ActivityResultContracts.OpenDocument())  result ->
        // do something 
    

在启动 Intent 时,只需添加您想要获取的 mime 类型

documentPick.launch(
            arrayOf(
                "application/pdf",
                "application/msword",
                "application/ms-doc",
                "application/doc",
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                "text/plain"
            )
        )

【讨论】:

谢谢!这正是我需要的:) android 文档尚未使用 registerForActivityResult 正确更新,它仍然显示 startActivityForResult :/ 您在哪里找到有关如何将 arrayOf() arg 传递给 launch 的文档?我一直在文档中搜索这种语法,似乎找不到任何接近的东西。

以上是关于将多个 Mime 类型传递给 ActivityResultLauncher.launch()的主要内容,如果未能解决你的问题,请参考以下文章

PowerShell:可以确定文件的 MIME 类型吗?

将相同输入类型的多个值传递给 BigQuery 表值函数

将模板类型传递给宏[重复]

以 DRY 方式将多个错误类传递给 ruby​​ 的救援子句

将不同类型的值传递给着色器

通过 onchange 事件将 SELECT 的值传递给 Javascript 函数?