I have a form where I need to attach a file from the phone. I have been looking for a file picker but it can only access images, not files like pdf, doc, docx, etc.
How to achieve this all in jetpack compose?
Asked
Active
Viewed 2,252 times
6

hippietrail
- 15,848
- 18
- 99
- 158

Aditya Sinha
- 61
- 6
1 Answers
9
According to documentation, it could be done with Intent.ACTION_OPEN_DOCUMENT
.
In Compose it you need rememberLauncherForActivityResult
to do it:
var pickedImageUri by remember { mutableStateOf<Uri?>(null) }
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
println("selected file URI ${it.data?.data}")
pickedImageUri = it.data?.data
}
pickedImageUri?.let {
Text(it.toString())
}
Button(
onClick = {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
.apply {
addCategory(Intent.CATEGORY_OPENABLE)
}
launcher.launch(intent)
}
) {
Text("Select")
}

Phil Dukhov
- 67,741
- 15
- 184
- 220
-
yup got it ... worked for me . – Aditya Sinha Apr 05 '22 at 19:29
-
1@AdityaSinha If this solved your question, please [accept it](https://meta.stackexchange.com/a/86979/794244) using the checkmark under the votes counter – Phil Dukhov Apr 06 '22 at 02:46