Display Ultra HDR images

The Ultra HDR image format lets images store more information about the intensity of light, resulting in more detailed highlights and shadows, and more intense colors. Android provides support for Ultra HDR images beginning with Android 14 (API level 34). If your app is running on those versions, it's important to configure your app to display these images properly. On the other hand, if your app isn't displaying Ultra HDR images, you can save device resources by not enabling Ultra HDR display. This page explains how to check whether graphics support Ultra HDR, and how to display them properly.

Check for the presence of a gain map

Ultra HDR images contain a gain map. The gain map is used to determine the increased brightness of each pixel in the image. To verify if an image is in the Ultra HDR format, convert the image or drawable into a Bitmap and call Bitmap.hasGainMap() (available since Android 14) to check if it has a gain map.

Configure your window to display Ultra HDR

To display Ultra HDR images with the full dynamic range, set the window's color mode to ActivityInfo.COLOR_MODE_HDR. Do this by calling the window's setColorMode() method. (These APIs are available from Android 8; however, images are not displayed in Ultra HDR unless the device is running Android 14 or higher.)

Putting it all together

The following code shows how the whole process looks. This code assumes an image is loaded into a Bitmap, and checks if the image has a gain map. If it does, the code switches the window's color mode to COLOR_MODE_HDR. If the image does not have a gain map, the code switches the window to the default color mode.

Kotlin

val bitmap = /* Get Bitmap from Image Resource */
binding.imageContainer.setImageBitmap(bitmap)

// Set color mode of the activity to the correct color mode.
requireActivity().window.colorMode =
   if (bitmap.hasGainmap()) ActivityInfo.COLOR_MODE_HDR else ActivityInfo.COLOR_MODE_DEFAULT

Java

final Bitmap bitmap = /* Get Bitmap from Image Resource */
binding.imageContainer.setImageBitmap(bitmap);

// Set color mode of the activity to the correct color mode.
int colorMode = ActivityInfo.COLOR_MODE_DEFAULT;
if (bitmap.hasGainmap()) colorMode = ActivityInfo.COLOR_MODE_HDR;
requireActivity().getWindow().setColorMode(colorMode);

Additional resources

To learn more about Ultra HDR images, see the following additional resources: