'pick only documents types from storage using activity result API
I need to make the user able to pick only documents types (ex: pdf, docs, xls) from phone storage using the new Activity Result API, but the problem is that when I launch the contract like the following code
private val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
...
}
fun pickDocument() = getContent.launch("*/*")
user become able to select any file type including images, videos, ...
Solution 1:[1]
Use
ActivityResultContracts.OpenDocument()
instead of
ActivityResultContracts.GetContent()
and pass the required mime-types in:
pickDocumentsContract.launch(arrayOf("mime-type-1","mime-type 2"))
example:
pickDocumentsContract.launch(arrayOf(
"application/msword", //.doc (Microsoft Word)
"application/pdf" //.pdf (Pdf file)
))
You can find more mime types here : https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
