Bitmap / Drawable / ImageDecoder

ImageDecoder – Error Handling, Cropping, & Scaling

The Android P developer preview contains a new ImageDecoder which is quite interesting and can do some useful things. At present it only appears within the Framework so we can only use it on devices running P or later, and I understand that if consists of a chunk of native code and so is unlikely to appear as a support library. However, it is a really nice, powerful yet simple API. In this series we’ll take a look at how to use it, and see some of the cool things that we can achieve with it.

Previously we’ve looked at the basics of how to use ImageDecoder, but often we have to plan for image data which may be outside of our control – and handle error conditions gracefully. The default behaviour is to throw an Exception and therefore we’ll never receive a return value from the decodeBitmap() or decodeDrawable() method. In Java you’ll receive an UnhandledException error at compile-time if you do not correctly handle this case, but Kotlin is less strict so the previous examples just work. However, we can easily add an exception handler if we want to properly detect error conditions:

This uses a corrupt PNG image which I deliberately corrupted by truncating it, and if we run this we get the following logcat output:

While this default behaviour works well in the majority of cases, there may be occasions where we need to tweak things, and we can do that quite easily. First we need to add an ImageDecoder.OnHeaderDecodedListener as we saw previously:

Within the body of this (which will be called as soon as the image header data has been parsed, but before the image data itself has been ready) we can register an ImageDecoder.OnPartialImageListener:

ImageDecoder.OnPartialImageListener receives two arguments: An error code Integer, and the ImageDecoder.Source object. The really useful thing is that we return a boolean value indicating whether the decoding should terminate by throwing an exception (i.e. the default behaviour). In this case we return false and so we get this default behaviour:

However, if we instead return true, then while the error inside the ImageDecoder.OnPartialImageListener will still be output, it will not longer throw an exception, and will actually return that partial image data that was loaded:

Another occurrence may be that an image may be a different size to what we actually need. We’ve already seen how the ImageDecoder.ImageInfo instance that we get inside the ImageDecoder.OnHeaderDecodedListener contains the size of the image in pixels, but we can also scale the image during processing so we get an image of the required dimensions:

In this case we halve the width and height to get a smaller image:

The other thing we can do is crop the image

This crops to the middle 400×400 pixels of the image:

So we can do some really quite useful processing during the image decoding to get our images resized to what we need, but there are some even more powerful things that we have. In the final article in this series we’ll dive even deeper in to the nice features offered by ImageDecoder.

The source code for this article is available here.

© 2018, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 ImageDecoder – Error Handling, Cropping, & Scaling by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

2 Comments

  1. I don’t some things here:
    1. What’s the advantage of using this code over Glide library? Can it decode more files ? VectorDrawable? Animated WEBP/HEIF ?
    2. Does it use some caching? Maybe even in OS scope ?
    3. What happens if I change orientation (making the activity get restarted) ?

    1. It’s doing a different job to Glide. ImageDecoder is purely about converting image data from a stream in to a Bitmap or Drawable. I know that Picasso 3.0 will use ImageDecoder internally, but I have no idea whether Glide will do the same – you would have to ask the authors of Glide that question.

Leave a Reply

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