24 avril 2023
Posted by Yacine Rezgui - Developer Relations Engineer
Improving privacy remains a top priority on Android. We've been investing in the platform to give users more control, increase transparency, and reduce the scope of access to private data.
Last year, we launched a new feature to emphasize this strategy: the Android photo picker. The photo picker is a browsable interface that presents the user with their media library, sorted by date from newest to oldest, and integrates nicely with your app’s experience without requiring media storage permissions!
It allows users to browse their photo gallery and grant access to specific items to an app. It’s a powerful tool allowing you to quickly add a photo selection feature to your apps without having to develop a complex in-house picker from scratch. It also eliminates the need to maintain complex logic for handling permissions and querying MediaStore, enabling you to save time and effort that would otherwise be spent on coding and debugging.
The photo picker is easy to implement, as you only need to include a few lines of code with the support library. Furthermore, it’s highly configurable, so you can customize the user experience according to your app’s specific needs.
One key piece of feedback we’ve heard from developers is the lack of support for older devices, making maintenance costly in terms of development. We are pleased to announce that, as part of the ActivityX 1.7.0 release, the Photo Picker support library will use a backported version provided by Google Play services on devices running Android KitKat (4.4) and later!
To enable the backported photo picker:
<!-- Prompt Google Play services to install the backported photo picker module -->
<service android:name="com.google.android.gms.metadata.ModuleDependencies" android:enabled="false" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.metadata.MODULE_DEPENDENCIES" />
</intent-filter>
<meta-data android:name="photopicker_activity:0:required" android:value="" />
</service>
Register an activity result with PickVisualMedia or PickMultipleVisualMedia and launch the photo picker.
// Register a variant of the photo picker where the user can select at most 5 items
val pickMultipleVisualMedia = registerForActivityResult(PickMultipleVisualMedia(5)) { uris ->
// Process URIs
Log.d("Photo Picker URIs count", uris.size)
}
// Launching the photo picker (photos & video included)
pickMultipleVisualMedia.launch(PickVisualMediaRequest(PickVisualMedia.ImageAndVideo))
Since our last blog post, we started rolling out support for the GET_CONTENT intent in the Android photo picker whenever the specified MIME type filter matches image/* and/or video/*. As the rollout will continue in the upcoming months, make sure to test your app once your device has the feature enabled:
adb shell device_config put storage_native_boot take_over_get_content true
Later this year, the photo picker will seamlessly support cloud storage providers like Google Photos, allowing users to select their remote content without having to leave your app, and without any code change on the developers side.
If you have any feedback or suggestions, submit tickets to our issue tracker.