androidkotlinandroid-jetpack-composefilechooserfilepicker

How do I start file chooser using intent in compose


I am trying to start file chooser on a button click (composable function). Unable to use startActivityForResult().

@Composable
fun SelectScreen() {

    Button(onClick = {
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        startActivity(intent)
    }
    ) {
        Text("BUTTON")
    }
}

Solution

  • Here is my suggestion:

    val pickPictureLauncher = rememberLauncherForActivityResult(
        ActivityResultContracts.GetContent()
    ) { imageUri ->
        if (imageUri != null) {
            // Update the state with the Uri
        }
    }
    
    // In your button's click
    pickPictureLauncher.launch("image/*")
    

    and in your composable which display the image, you can do the following

    val image = remember {
       // Make sure to resize and compress
       // the image to avoid display a big bitmap
       ImageUtils.imageFromUri(imageUi)
    }
    Image(
       image,
       contentDescription = null
    )