23

Photo picker  |  Android 13 Developer Preview  |  Android Developers

 2 years ago
source link: https://developer.android.com/about/versions/13/features/photopicker
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Photo picker

Photo picker dialogue appears with media files on your device. Select a photo to share with the app.Figure 1. Photo picker provides an intuitive UI for sharing photos with your app.

Android 13 includes support for a new photo picker tool. This tool provides a safe, built-in way for users to select media files, without needing to grant your app access to their entire media library.

Note: Upcoming Google Play system updates are expected to include new features related to the photo picker. In one such update, the library will add support for apps that target Android 11 (API level 30) or higher (excluding Android Go devices).

Media selection

The photo picker provides a browsable, searchable interface that presents the user with their media library, sorted by date (from newest to oldest). You can specify that users should see only photos or only videos, and the maximum number of media selections allowed by default is set to 1.

Define sharing limitations

Apps can declare a value for android.provider.extra.PICK_IMAGES_MAX, which indicates the maximum number of media files that appear in the photo picker when shown to the user. For instance, if you prompt a user to select a required profile picture for their account, set one photo as a maximum sharing requirement.

Note: If you choose a maximum of 1, the photo picker opens in half-screen mode.

To launch photo picker in single-select mode, do the following:

// Launches photo picker in single-select mode.
// This means that the user can select one photo or video.
val intent = Intent(MediaStore.ACTION_PICK_IMAGES)
startActivityForResult(intent, PHOTO_PICKER_REQUEST_CODE)

Select multiple photos or videos

If your app’s use case requires that the user select multiple photos or videos, you can specify the maximum number of images that should appear in the photo picker using the EXTRA_PICK_IMAGES_MAX extra, as shown in the following code snippet:

// Launches photo picker in multi-select mode.
// This means that user can select multiple photos/videos, up to the limit
// specified by the app in the extra (10 in this example).
val maxNumPhotosAndVideos = 10
val intent = Intent(MediaStore.ACTION_PICK_IMAGES)
intent.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, maxNumPhotosAndVideos)
startActivityForResult(intent, PHOTO_PICKER_MULTI_SELECT_REQUEST_CODE)

Keep in mind that there is a platform limit on the largest number you can specify as a maximum number of files. To access this limit, call MediaStore#getPickImagesMaxLimit().

Handle the photo picker results

After the photo picker launches, use the new ACTION_PICK_IMAGES intent to handle the results. The picker returns a set of URIs:

// onActivityResult() handles callbacks from the photo picker.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode != Activity.RESULT_OK) {
        // Handle error
return
    }
    when (requestCode) {
            REQUEST_PHOTO_PICKER_SINGLE_SELECT -> {
            // Get photo picker response for single select.
            val currentUri: Uri = data.data

// Do stuff with the photo/video URI.
            return
        }
                REQUEST_PHOTO_PICKER_MULTI_SELECT -> {
            // Get photo picker response for multi select.
            var i = 0
            while (i < data.clipData!!.itemCount) {

By default, the photo picker shows both photos and videos. You can also filter by only photos or only videos by setting a MIME type in the setType() method. For example, to show only videos in the photo picker, pass video/* into setType():

// Launches photo picker for videos only in single select mode.
val intent = Intent(MediaStore.ACTION_PICK_IMAGES)
intent.type = "video/*"
startActivityForResult(intent, PHOTO_PICKER_VIDEO_SINGLE_SELECT_REQUEST_CODE)

// Apps can also change the mimeType to allow users to select
// images only - intent.type = "images/*"

Note: Your app doesn’t have persistent access to the URI returned from this intent. When your app’s process ends, your app loses access to the URI.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK