Fotoğraf seçici

Cihazınızdaki medya dosyalarını içeren fotoğraf seçici iletişim kutusu görüntülenir. Uygulamayla paylaşılacak bir fotoğraf seçin.
Şekil 1. Fotoğraf seçici, uygulamanızla fotoğraf paylaşmanız için sezgisel bir kullanıcı arayüzü sağlar.

Fotoğraf seçici, kullanıcıya medya kitaplığını tarihe göre yeniden eskiye doğru sıralanmış şekilde sunan göz atılabilir bir arayüz sağlar. Gizlilikle ilgili en iyi uygulamalar codelab'de gösterildiği gibi, fotoğraf seçici, kullanıcıların uygulamanızın tüm medya kitaplıkları yerine yalnızca seçili resimlere ve videolara erişmesine izin vermesi için güvenli ve yerleşik bir yöntem sunar.

Cihazlarında uygun bulut medya sağlayıcıları bulunan kullanıcılar, uzaktan depolanan fotoğraf ve videolar arasından da seçim yapabilir. Bulut medya sağlayıcıları hakkında daha fazla bilgi edinin.

Bu araç otomatik olarak güncellenerek herhangi bir kod değişikliği yapmanıza gerek kalmadan uygulamanızın kullanıcılarına zaman içinde daha fazla işlev sunar.

Jetpack Activity sözleşmelerini kullanma

Fotoğraf seçici entegrasyonunu kolaylaştırmak için androidx.activity kitaplığının 1.7.0 veya sonraki bir sürümünü ekleyin.

Fotoğraf seçiciyi başlatmak için aşağıdaki etkinlik sonucu sözleşmelerini kullanın:

Fotoğraf seçici bir cihazda yoksa kitaplık bunun yerine otomatik olarak ACTION_OPEN_DOCUMENT amaç işlemini çağırır. Bu amaç, Android 4.4 (API düzeyi 19) veya sonraki sürümleri çalıştıran cihazlarda desteklenir. isPhotoPickerAvailable() numaralı telefonu arayarak fotoğraf seçicinin belirli bir cihazda kullanılıp kullanılamadığını doğrulayabilirsiniz.

Tek bir medya öğesi seçin

Tek bir medya öğesi seçmek için aşağıdaki kod snippet'inde gösterildiği gibi PickVisualMedia etkinlik sonucu sözleşmesini kullanın:

Kotlin

// Registers a photo picker activity launcher in single-select mode.
val pickMedia = registerForActivityResult(PickVisualMedia()) { uri ->
    // Callback is invoked after the user selects a media item or closes the
    // photo picker.
    if (uri != null) {
        Log.d("PhotoPicker", "Selected URI: $uri")
    } else {
        Log.d("PhotoPicker", "No media selected")
    }
}

// Include only one of the following calls to launch(), depending on the types
// of media that you want to let the user choose from.

// Launch the photo picker and let the user choose images and videos.
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.ImageAndVideo))

// Launch the photo picker and let the user choose only images.
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))

// Launch the photo picker and let the user choose only videos.
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.VideoOnly))

// Launch the photo picker and let the user choose only images/videos of a
// specific MIME type, such as GIFs.
val mimeType = "image/gif"
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.SingleMimeType(mimeType)))

Java

// Registers a photo picker activity launcher in single-select mode.
ActivityResultLauncher<PickVisualMediaRequest> pickMedia =
        registerForActivityResult(new PickVisualMedia(), uri -> {
    // Callback is invoked after the user selects a media item or closes the
    // photo picker.
    if (uri != null) {
        Log.d("PhotoPicker", "Selected URI: " + uri);
    } else {
        Log.d("PhotoPicker", "No media selected");
    }
});

// Include only one of the following calls to launch(), depending on the types
// of media that you want to let the user choose from.

// Launch the photo picker and let the user choose images and videos.
pickMedia.launch(new PickVisualMediaRequest.Builder()
        .setMediaType(PickVisualMedia.ImageAndVideo.INSTANCE)
        .build());

// Launch the photo picker and let the user choose only images.
pickMedia.launch(new PickVisualMediaRequest.Builder()
        .setMediaType(PickVisualMedia.ImageOnly.INSTANCE)
        .build());

// Launch the photo picker and let the user choose only videos.
pickMedia.launch(new PickVisualMediaRequest.Builder()
        .setMediaType(PickVisualMedia.VideoOnly.INSTANCE)
        .build());

// Launch the photo picker and let the user choose only images/videos of a
// specific MIME type, such as GIFs.
String mimeType = "image/gif";
pickMedia.launch(new PickVisualMediaRequest.Builder()
        .setMediaType(new PickVisualMedia.SingleMimeType(mimeType))
        .build());

Birden çok medya öğesi seçin

Birden fazla medya öğesi seçmek için, aşağıdaki kod snippet'inde gösterildiği gibi seçilebilir maksimum medya dosyası sayısı ayarlayın.

Kotlin

// Registers a photo picker activity launcher in multi-select mode.
// In this example, the app lets the user select up to 5 media files.
val pickMultipleMedia =
        registerForActivityResult(PickMultipleVisualMedia(5)) { uris ->
    // Callback is invoked after the user selects media items or closes the
    // photo picker.
    if (uris.isNotEmpty()) {
        Log.d("PhotoPicker", "Number of items selected: ${uris.size}")
    } else {
        Log.d("PhotoPicker", "No media selected")
    }
}

// For this example, launch the photo picker and let the user choose images
// and videos. If you want the user to select a specific type of media file,
// use the overloaded versions of launch(), as shown in the section about how
// to select a single media item.
pickMultipleMedia.launch(PickVisualMediaRequest(PickVisualMedia.ImageAndVideo))

Java

// Registers a photo picker activity launcher in multi-select mode.
// In this example, the app lets the user select up to 5 media files.
ActivityResultLauncher<PickVisualMediaRequest> pickMultipleMedia =
        registerForActivityResult(new PickMultipleVisualMedia(5), uris -> {
    // Callback is invoked after the user selects media items or closes the
    // photo picker.
    if (!uris.isEmpty()) {
        Log.d("PhotoPicker", "Number of items selected: " + uris.size());
    } else {
        Log.d("PhotoPicker", "No media selected");
    }
});

// For this example, launch the photo picker and let the user choose images
// and videos. If you want the user to select a specific type of media file,
// use the overloaded versions of launch(), as shown in the section about how
// to select a single media item.
pickMultipleMedia.launch(new PickVisualMediaRequest.Builder()
        .setMediaType(PickVisualMedia.ImageAndVideo.INSTANCE)
        .build());

Platformda, kullanıcıdan fotoğraf seçicide seçim yapmasını isteyebileceğiniz maksimum dosya sayısı sınırlıdır. Bu sınıra erişmek için getPickImagesMaxLimit() numaralı telefonu arayın. Fotoğraf seçicinin desteklenmediği cihazlarda bu sınır yok sayılır.

Uygun cihazlar

Fotoğraf seçici, aşağıdaki ölçütleri karşılayan cihazlarda kullanılabilir:

Android 4.4 (API düzeyi 19) ile Android 10 (API düzeyi 29) arasındaki sürümleri çalıştıran eski cihazlar ve Google Play Hizmetleri'ni destekleyen Android 11 veya 12 yüklü Android Go cihazlar, fotoğraf seçicinin geri bağlanmış bir sürümünü yükleyebilir. Geri taşınan fotoğraf seçici modülünün Google Play Hizmetleri üzerinden otomatik olarak yüklenmesini etkinleştirmek için uygulamanızın manifest dosyasındaki <application> etiketine aşağıdaki girişi ekleyin:

<!-- Trigger 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"
         tools:ignore="MissingClass">
    <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>

Medya dosyası erişimini sürdür

Varsayılan olarak sistem, cihaz yeniden başlatılana veya uygulamanız durana kadar uygulamanızın medya dosyalarına erişmesine izin verir. Uygulamanız, arka planda büyük bir dosya yüklemek gibi uzun süreli çalışmalar gerçekleştiriyorsa bu erişimin daha uzun süre devam etmesi gerekebilir. Bunun için takePersistableUriPermission() yöntemini çağırın:

Kotlin

val flag = Intent.FLAG_GRANT_READ_URI_PERMISSION
context.contentResolver.takePersistableUriPermission(uri, flag)

Java

int flag = Intent.FLAG_GRANT_READ_URI_PERMISSION;
context.contentResolver.takePersistableUriPermission(uri, flag);