Search code examples
androidandroid-intentandroid-photo-picker

How to enable Browse option in Photo Picker Android


When I implement a Photo Picker functionality in my app using Photo Picker Android library. I have access only Camera, Screenshot and Download folders including some recent photos/videos, Not the complete access. But in one of the google implemented app (Snapseed), Same kind of photo picker can access the Intent.ACTION_GET_CONTENT picker to acess other directories after clicking on browse button. For reference I have attached a screenshot

Can anyone please help me, how to achieve the browse button in native android photo picker?


Solution

  • I was able to get the "Browse" option by using ActivityResultContracts.GetContent() instead of ActivityResultContracts.PickVisualMedia(). What this is effectively doing is creating the following Intent:

    Intent(Intent.ACTION_GET_CONTENT)
                    .addCategory(Intent.CATEGORY_OPENABLE)
                    .setType("image/*")
    

    I believe you can launch that intent directly or my code is using the ActivityResultContracts paradigm inside of a composable so it looks like this:

    val photoPickerLauncher =
           rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
                //do something with image uri
            }
    
    photoPickerLauncher.launch(MIME_TYPE_IMAGE)
    
    

    Previously I was using ActivityResultContracts.PickVisualMedia() as outlined in the docs. With that, I wasn't getting the browse option on the photo picker. With the code above, I do. I also tested on an Android 11 device and it works fine using the old gallery UI.

    Now I can't really explain why this works so use at your own risk. I find documentation for PhotoPicker and Gallery selection in general very poor. But this seemed to work for me.