我们如何在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?
您需要将 MIME 类型添加到意图中:
intent.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/png", "application/pdf"))
我不能 100% 确定这些 MIME 类型是否正确
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"))
}
您可以通过重写
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("*/*")
}