How to access files from shared storage in Jetpack Compose?
Asked Answered
R

1

7

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?

Recite answered 2/4, 2022 at 16:49 Comment(0)
C
12

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")
}
Coth answered 3/4, 2022 at 3:41 Comment(2)
yup got it ... worked for me .Recite
@AdityaSinha If this solved your question, please accept it using the checkmark under the votes counterCoth

© 2022 - 2024 — McMap. All rights reserved.