Blog Infos
Author
Published
Topics
,
Author
Published

Android 13 (API 33) introduces a new tool called photo picker 🖼. Today, we will learn how to integrate this into android applications.

 

It provides a browsable, searchable interface that presents the user with their media library, sorted by date 📆.

Note from official docs:

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

Major features
  • Safe, and a built-in method for users to select media files(photos/videos)
  • No permissions are required 💃
Media selection limit
  • By default maximum number of media selections allowed is set to 1.

🛑 During development, I found weird behavior when defining the maximum number 1, it immediately fails with RESULT_CANCELED(0) 🤯

  • No need to worry we can modify it 😊

Provide the limit using the putExtra() method of Intent

EXTRA_PICK_IMAGES_MAX // "android.provider.extra.PICK_IMAGES_MAX"
Example
Intent(MediaStore.ACTION_PICK_IMAGES).apply {
putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, 2) // deafult is 1
}

ℹ️ There is a platform limit on the largest number that needs to be respected when we specify the maximum number of files.

  • MediaStore.getPickImagesMaxLimit() provides that limit.
val maxLimit = MediaStore.getPickImagesMaxLimit()

The photo picker opens in half-screen mode when the maximum is set to 1

  • Photo picker screenshot

 

Left side (default limit 1), Right side (when the limit is more than 1)

Restricting MimeType
  • By default, the photo picker displays both photos 🖼 and videos 📹
  • No need to worry we can restrict it to photos or videos only 😊

Provide the MimeType by using the setType() method of Intent

Intent(MediaStore.ACTION_PICK_IMAGES).apply {
type = "image/*"
// only videos
type = "video/*"
}
view raw SetMimeType.kt hosted with ❤ by GitHub
Let’s code it
  • XML-based UI, Photo picker, and ActivityResult API
// create ActivityResultContract
val getContent =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
result?.let { nonNullableResult ->
when (nonNullableResult.resultCode == RESULT_OK) {
true -> {
val intent = nonNullableResult.data
intent?.data?.let { uri ->
// process data
}
}
false -> {
}
}
}
}
// launch the intent on button click
binding.fab.setOnClickListener {
Log.d(TAG, "onCreate:${MediaStore.getPickImagesMaxLimit()}")
val intent = Intent(MediaStore.ACTION_PICK_IMAGES).apply {
type = "image/*"
}
getContent.launch(intent)
}
  • Compose, Photo picker, and ActivityResult API
@Composable
fun PhotoPickerResultComposable() {
var result by rememberSaveable { mutableStateOf<Uri?>(null) }
val launcher =
rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result = it.data?.data
}
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
OpenPhotoPicker(openLauncher = { launcher.launch(photoIntent) })
AsyncImage(
model = result,
contentDescription = "Image from photo picker",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(200.dp, 200.dp)
.clip(CircleShape)
)
}
}

Job Offers

Job Offers


    Android Developer

    ClearScore
    London (Hybrid role)
    • Full Time
    apply now

    Mobile Developer Android (m/w/d) Firebase

    REWE digital
    Köln / Cologne
    • Full Time
    apply now

    Android Manager (all genders)

    Babbel
    Berlin
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

The Evolution of Android Graphics in Android 12/13

Android 12 and 13 both added significant new capabilities to Android platform graphics, including RenderEffect, RuntimeShader, and more. At the same time, RenderScript has been deprecated and we’ve introduced the RenderScript Intrinsics Replacement Toolkit. This…
Watch Video

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocate and Fast Talking YouTuber
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Ad ...
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocat ...
Google

Jobs

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Today, We will explore the New notification 🔔 runtime permission that was added in…
READ MORE
blog
How to use the new feature from Android 13 to display your app in…
READ MORE
blog
In the recently published Android 13 Developer Preview blog post, Dave Burke noted two…
READ MORE
blog
Google recently announced the first Developer Preview of Android 13, while there are a…
READ MORE

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu