코딩하는 일용직 노동자

Photo Picker - Android13 본문

안드로이드

Photo Picker - Android13

bacass 2022. 2. 14. 14:47
Android 13 Preview에 새로운 사진 선택 도구가 포함되었습니다.
이 라이브러리는 향후 Android 11(API 레벨 30) 이상의 앱을 지원합니다.
앱에 전체 미디어 라이브러리에 대한 액세스 권한을 부여하지 않고도 사용자가 미디어 파일을 선택할 수 있는 안전한 기본 제공 방법을 제공해줍니다.
사용자가 사진 또는 비디오만 볼 수 있도록 지정할 수 있으며 설정에 따라 1장만 선택할 수도, 여러개의 사진을 선택할 수도 있습니다.

- 사진 1장만 선택할때

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

 

- 여러장을 선택할때

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

- 선택한 사진은 onActivityResult() 를 통해 전달받습니다.

// 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;
    }
}

- 동영상만 선택할때

// 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("images/*");
// or a specific mimeType - intent.setType("image/gif");

https://developer.android.com/about/versions/13/features/photopicker

 

Photo picker  |  Android 13 Developer Preview  |  Android Developers

Welcome to the Android 13 Developer Preview! Please give us your feedback, and help us make Android 13 the best release yet. Photo picker Figure 1. Photo picker provides an intuitive UI for sharing photos with your app. Android 13 includes support for a n

developer.android.com