사진 선택 도구

기기의 미디어 파일과 함께 표시되는 사진 선택 도구 대화상자 앱에 공유할 사진 선택
그림 1. 앱에 사진을 공유할 수 있는 직관적인 UI를 제공하는 사진 선택 도구

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)

자바

// 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 추가 항목을 사용하여 사진 선택 도구에 표시되어야 하는 최대 이미지 수를 지정하면 됩니다.

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)

자바

// 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 인텐트를 사용하여 결과를 처리합니다. 선택 도구는 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) {

자바

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

자바

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