Skip to content

Photo Picker

modernstorage-photopicker is a library providing an ActivityResultContract for the ActivityResult API to launch the Photo Picker intent when available on device or rely on the existing system file picker using the ACTION_OPEN_DOCUMENT intent. It works on Android 21+.

Info

The Photo Picker feature is part of Android 13. The backport to Android 11 & 12 will be released this year and modernstorage-photopicker will be able to support it with an update without any code changes on your side

Add dependency to project

modernstorage-photopicker is available on mavenCentral().

// build.gradle
implementation("com.google.modernstorage:modernstorage-photopicker:1.0.0-alpha06")

API reference

modernstorage-photopicker API reference is available here.

How to use the photo picker

@Composable
fun PhotoPickerExample() {
    // Register a callback for the Activity Result
    val photoPicker = rememberLauncherForActivityResult(PhotoPicker()) { uris ->
        // uris contain the list of selected images & video
        println(uris)
    }

    Column {
        Button(onClick = {
            // Launch the picker with only one image selectable
            photoPicker.launch(PhotoPicker.Args(PhotoPicker.Type.IMAGES_ONLY, 1))
        }) {
            Text("Select 1 image max")
        }

        Button(onClick = {
            // Launch the picker with 15 video selectable
            photoPicker.launch(PhotoPicker.Args(PhotoPicker.Type.VIDEO_ONLY, 15))
        }) {
            Text("Select 15 video max")
        }

        Button(onClick = {
            // Launch the picker with 5 max images & video selectable
            photoPicker.launch(PhotoPicker.Args(PhotoPicker.Type.IMAGES_AND_VIDEO, 5))
        }) {
            Text("Select 5 images & video max")
        }
    }
}
// Register a callback for the Activity Result
val photoPicker = registerForActivityResult(PhotoPicker()) { uris ->
    // uris contain the list of selected images & video
    println(uris)
}


// Launch the picker with only one image selectable
photoPicker.launch(PhotoPicker.Args(PhotoPicker.Type.IMAGES_ONLY, 1))

// Launch the picker with 5 video selectable
photoPicker.launch(PhotoPicker.Args(PhotoPicker.Type.VIDEO_ONLY, 5))

// Launch the picker with 15 max images & video selectable
photoPicker.launch(PhotoPicker.Args(PhotoPicker.Type.IMAGES_AND_VIDEO, 15))