首页 > 解决方案 > 打开带有扩展过滤器的文件对话框,用于使用 kotlin 进行自定义扩展

问题描述

有没有办法为此意图设置文件扩展名过滤器:我想过滤带有此扩展名“.xxx”的文件

val intent = Intent()
                .setType("*/*") // <- it doesn't work if I put it here (".xxx")
                .setAction(Intent.ACTION_GET_CONTENT)
            startActivityForResult(Intent.createChooser(intent, "Select a file (.xxx)"), OPEN_FILE_CODE)

谢谢!

标签: androidkotlinfilteropenfiledialog

解决方案


有没有办法为此意图设置文件扩展名过滤器

不,对不起。

it doesn't work if I put it here (".xxx")

那是因为setType()采用 MIME 类型,而不是文件扩展名。如果您的文件扩展名是常见的并且映射到众所周知的 MIME 类型,您可以尝试将该 MIME 类型与setType().


推荐阅读