照片选择器

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。
照片选择器对话框中会显示您设备上的媒体文件。您可选择要与应用分享的照片。
图 1. 照片选择器提供了一个直观的界面,便于与您的应用分享照片。

Android 13(API 级别 33)支持新的照片选择器工具。此工具为用户提供了一种安全的内置媒体文件选择方式,让其无需向应用授予对整个媒体库的访问权限。

选择媒体

照片选择器提供了一个可浏览、可搜索的界面,其中按日期(从最近到最早)顺序向用户呈现其媒体库中的文件。您可以指定用户只能看到照片或只能看到视频,并且默认情况下,允许的媒体选择量上限设置为 1。

定义分享限制

应用可以声明 android.provider.extra.PICK_IMAGES_MAX 的值,该值表示在向用户显示时照片选择器中显示的媒体文件数量上限。例如,如果您提示用户为其帐号选择要求的个人资料照片,请将一张照片设置为分享要求上限。

如需在单选模式下启动照片选择器,请执行以下操作:

Kotlin

// 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)

Java

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

选择多张照片或多个视频

如果应用的用例需要用户选择多张照片或多个视频,您可以使用 EXTRA_PICK_IMAGES_MAX extra 指定照片选择器中应显示照片的数量上限,如以下代码段中所示:

Kotlin

// 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)

Java

// 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).
final int maxNumPhotosAndVideos = 10;
Intent intent = new Intent(MediaStore.ACTION_PICK_IMAGES);
intent.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, maxNumPhotosAndVideos);
startActivityForResult(intent, PHOTO_PICKER_MULTI_SELECT_REQUEST_CODE);

请注意,可指定为文件数量上限的最大数字存在平台限制。如需访问此限制,请调用 MediaStore#getPickImagesMaxLimit()

处理照片选择器结果

照片选择器启动后,使用新的 ACTION_PICK_IMAGES intent 来处理结果。该选择器会返回一组 URI:

Kotlin

// 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) {

Java

// onActivityResult() handles callbacks from the photo picker.
@Override
protected void onActivityResult(
    int requestCode, int resultCode, final Intent data) {

    if (resultCode != Activity.RESULT_OK) {
        // Handle error
        return;
    }

    switch(requestCode) {
        case REQUEST_PHOTO_PICKER_SINGLE_SELECT:
            // Get photo picker response for single select.
            Uri currentUri = data.getData();

            // Do stuff with the photo/video URI.
            return;
        case REQUEST_PHOTO_PICKER_MULTI_SELECT:
            // Get photo picker response for multi select
            for (int i = 0; i < data.getClipData().getItemCount(); i++) {
                Uri currentUri = data.getClipData().getItemAt(i).getUri();

                // Do stuff with each photo/video URI.
            }
            return;
    }
}

默认情况下,照片选择器会既显示照片又显示视频。您还可以在 setType() 方法中设置 MIME 类型,以便按“仅显示照片”或“仅显示视频”进行过滤。例如,如需在照片选择器中仅显示视频,请将 video/* 传入 setType()

Kotlin

// 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/*"

Java

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

// Apps can also change the mimeType to allow users to select
// images only - intent.setType("image/*");
// or a specific mimeType - intent.setType("image/gif");