Blog Infos
Author
Published
Topics
Author
Published
Topics

 

On the 9th of January 2023, I read this post on Android Developers Blog which sounds pretty interesting.

Extending the Android SDK

The Modular System Components (MSCs) in Android 10 and higher enable backward compatibility between new functionality and previously-released Android versions.

Android team released a new framework called SDKExtensions which helps to make development more flexible using new APIs that will be available for older Android versions.

Using this framework helps developers integrate new APIs like Photo picker, introduced in Android 13with older versions too, which ultimately leads to a better user experience and increases developer productivity.

Checking which SDK Extensions are available on a device using adb
adb shell getprop | grep build.version.extensions
Using Photo Picker APIs on older versions with SDKExtensions
Check which SDKExtensions version is required
ACTION_PICK_IMAGE

 

Install the SDK tools to get started with SDKExtensions
  • Using Android Studio, open the SDK Manager, and install the Android SDK Platform entry with the corresponding extension level
  • In our case, we need API 33 with an Extension level ≥ 2

 

Configure the project to get started with SDKExtensions
  • app’s build.gradle.kts or build.gradle
android {
    compileSdk = 33
    compileSdkExtension = 4
    ...
}
Photo picker APIs are only available for Android ≥13

🙏 Thanks to SDKExtensions because now it’s available all the way back to API 30(Android R/11) 🎉

  • Before SDKExtensions we check the availability of PhotoPicker API using the following code 🧑‍💻👩‍💻:
  • It returns true if the Android version is ≥13 which restricts the usage of this API only to ≥13 😢
// Older way of checking the availability of PhotoPicker
fun isPhotoPickerAvailable(): Boolean = 
                 Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
  • Here are some stats 📈 from the Android studio’s new project wizard about the devices that are running Android 11/13

 

Android 30(11) and Android 33(30) usage stats from new project wizard- Android Studio

 

  • As you can see, SDKExtensions really help us to cover almost 50% of the devices 💚 when it comes to providing the feature for selecting images/videos using Photo picker APIs

We have 2options for implementing Photo Picker API

  1. Manually check the availability of an API
  2. Using ActivityResultContracts
Manual implementation
  1. Check if the photo picker is available or not
fun isPhotoPickerAvailable(): Boolean =
    when {
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> true

        Build.VERSION.SDK_INT >= Build.VERSION_CODES.R &&
            SdkExtensions.getExtensionVersion(Build.VERSION_CODES.R) >= 2 -> true

        else -> {
            false
        }
    }

2. startActivityForResult and proceed accordingly or Use ActivityResult API which is recommended way

Using ActivityResultContracts.PickVisualMedia/PickMultipleVisualMedia
  • It provides a public companion function called isPhotoPickerAvailable() that helps us to check the availability of the Photo picker API
  • Here is the code for this method:

As you can see it first check if the SDK version is 13if not than it checks the SDKExtension versionswhich helps developers to use it for older devices

@JvmStatic
fun isPhotoPickerAvailable(): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        true
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        // getExtension is seen as part of Android Tiramisu only while the SdkExtensions
        // have been added on Android R
        getExtensionVersion(Build.VERSION_CODES.R) >= 2
    } else {
        false
    }
}
  • Using this method to check the availability of the PhotoPicker
fun isPhotoPickerAvailable(): Boolean = 
              ActivityResultContracts.PickVisualMedia.isPhotoPickerAvailable()
i̵s̵P̵h̵o̵t̵o̵P̵i̵c̵k̵e̵r̵A̵v̵a̵i̵l̵a̵b̵l̵e̵(̵)̵ — Deprecated🤯

Starting from Activity version 1.7.0-alpha04

  • We got a new version of isPhotoPickerAvailable(context:Context) which has the additional check to see if the device has support for the photo picker by checking the running Android version, the SDK extension version or the picker provided by Google Play services
fun isPhotoPickerAvailable(context: Context): Boolean {
    return ActivityResultContracts.PickVisualMedia.isPhotoPickerAvailable(context = context)
}
Composable to pick an image 🌅
@Composable
fun PhotoPickerResultComposable() {
    val context = LocalContext.current

    var result by rememberSaveable { mutableStateOf<Uri?>(null) }

    val pickMedia = rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
        // Callback is invoked after the user selects a media item or closes the
        // photo picker.
        when {
            uri != null -> {
                Log.d(TAG, "PhotoPickerResultComposable:$uri")
                result = uri
            }

            else -> {
                Log.d(TAG, "No image 🌅 selected")
            }
        }
    }

    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        OpenPhotoPicker(
            openLauncher = {
                when {
                    ActivityResultContracts.PickVisualMedia.isPhotoPickerAvailable(context = context) -> pickMedia.launch(
                        PickVisualMediaRequest(
                            ActivityResultContracts.PickVisualMedia.ImageOnly,
                        ),
                    )

                    else -> Toast.makeText(context, "Photo picker not available", Toast.LENGTH_SHORT).show()
                }
            },
        )

        AsyncImage(
            model = result,
            contentDescription = "Image from photo picker",
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(200.dp, 200.dp)
                .clip(CircleShape),
        )
    }
}

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Designing for Errors in a Kotlin-first SDK:When to throw, nullify, or return a Result.

Error propagation is an important consideration for building feature-rich API architecture in Kotlin. Whereas apps may have the flexibility to add external dependencies and laser-focus on specific use-cases, designing an API requires minimal dependencies and…
Watch Video

Designing for Errors in a Kotlin-first SDK:When to throw, nullify, or return a Result.

Hudson Miears
Engineer, ArcGIS Maps SDK for Kotlin Team
Esri

Designing for Errors in a Kotlin-first SDK:When to throw, nullify, or return a Result.

Hudson Miears
Engineer, ArcGIS Map ...
Esri

Designing for Errors in a Kotlin-first SDK:When to throw, nullify, or return a Result.

Hudson Miears
Engineer, ArcGIS Maps SDK ...
Esri

Jobs

No results found.

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Well, you’ve read the title of the article so there will be no secret…
READ MORE
blog
Since August 5, 2020 (decree № 2020–983), segmented advertising has been authorized on television…
READ MORE
blog
In May 2022, Google announced Google Play SDK Index. This is part of their…
READ MORE
blog
Earlier this year Google announced its Privacy Sandbox for Android which aims to build…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Menu