Implement ImageDecoder API in Android
Last Updated :
15 Sep, 2022
We use a lot of Bitmaps and drawables in Android. Handling bitmap conversions necessitates a significant amount of code, and we frequently encounter the favorite error, the "Out of Memory" exception. The BitmapFactory is used to manipulate Bitmaps, but with Android P, we have ImageDecoder, which allows us to convert images like PNG, JPEG, and so on to Drawables or Bitmaps. We will now go through all the below-mentioned topics in detail:
- Understanding the source and loading Images
- Decoding from the Drawable folder
- URI Overriding the source's default settings
- Decoding GIFs and WebP Error Handling
Understanding the Source and Image Loading
Before we can decode anything, we must first map the image source. The source is equivalent to the ImageDecoder's accepted path. We use to create a source
val gfg = ImageDecoder.createSource(file_path_of_your_image)
In this case, generating a source can happen on any thread. However, decoding should be done in the background thread.
imageView.setImageDrawable(your_drawable_file)
We're using the decodeDrawable method to acquire a drawable, but we'll use the decodeBitmap function to get a bitmap from the specified source.
val bmp:Bitmap = ImageDecoder.decodeBitmap(your_source_file)
The preceding use-case was to generate a source from a file path and decode it to Drawable or Bitmap. Similarly, we can construct a source from ByteBuffer as follows:
val file = ImageDecoder.createSource(byte_files)
Drawable Folder and URI Decoding
Consider a scenario in which we have PNGs or JPEGs in our project's drawable folder. Then we can make a source from the resource folder, for example,
val file = ImageDecoder.createSource(resources, R.drawable.gfg_logo)
We're using ImageDecoder to get the PNG icon ic location from the drawable and create a source for it. Now we may use Drawable or Bitmap to decode the source.
val file: Drawable = ImageDecoder.decodeDrawable(source_file)
SetImageDrawable and setImageBitmap can be used to convert these to ImageView. Similarly, if we have a URI and wish to make a source out of it, we use a content resolver to do so.
val file_source = ImageDecoder.createSource(contentResolver, image_uri)
Finally, if we need to make a source from a file from an asset, we use it.
val file_name = ImageDecoder.createSource(assetManager, some_asset)
Overriding the Source's Default Settings
We can override the default settings we get from the Image while creating a source. We utilize this to alter the default configuration. We use the onHeaderDecodedListener to add the listener.
Kotlin
val gfgListener: OnHeaderDecodedListener = object : OnHeaderDecodedListener {
override fun onHeaderDecoded(decoder: ImageDecoder, info: ImageInfo, source: ImageDecoder.Source) {
// Your logic here.
}
}
val img_drawable = ImageDecoder.decodeDrawable(your_source, listener)
The listeners can be used for a variety of transformations
The decoder allows us to do transformations, while the information holds all of the original image's data, such as Mime type, size, and whether or not it is animating, as well as the source. Consider what we would put inside the onHeaderDecoded function if we wanted to resize the image.
decoder.setTargetSize(50,50)
WebP and GIF decoding
If we have GIFs and WebP files, we can load them with all of the frames' animations and transitions using ImageDecoder alone, without the need for a third-party library. Let's say we have a Gif file as a source from the assets folder. So, in order to decode it in Drawable and begin the animation
val img_source = ImageDecoder.createSource(assetManager, your_asset_file)
Kotlin
val img_drawable = ImageDecoder.decodeDrawable(img_source)
if (img_source is AnimatedImageDrawable) {
drawable.start()
}
Error Handling
We may encounter issues while decoding the source. To detect issues, we must set the decoder argument in OnHeaderDecodedListener to setOnPartialImageListener, as seen below.
Kotlin
val gfgListener: OnHeaderDecodedListener = object : OnHeaderDecodedListener {
override fun onHeaderDecoded(decoder: ImageDecoder, info: ImageInfo, your_source: ImageDecoder.Source) {
decoder.setOnPartialImageListener {exception->
Log.d("GfG Decoder",exception.error.toString()))
true
}
}
}
We get the exception here, inside setOnPartialImageListener, and that's where we can log the error. When we want to log an error, exception.error may return one of the following errors:
We're returning true in this case, which means the listeners should only see the created image until the exception occurs. If it returns false, however, it will abort the execution and throw an exception.
It's good to know
We can apply some processing once the image has been loaded, such as adding a custom background, etc.
OnHeaderDecodedListener
We use it for processing in the following ways:
Kotlin
val gfgListener: OnHeaderDecodedListener = object : OnHeaderDecodedListener {
override fun onHeaderDecoded(decoder: ImageDecoder, info: ImageInfo, source: ImageDecoder.Source) {
decoder.setPostProcessor { canvas ->
}
}
}
Here, under setOnProcessor, we obtain the canvas on which we will perform our changes and apply custom effects when the Image has been decoded and loaded. This is how ImageDecoder can be used in your application. To run in your project, you'll need Android Pie or higher.
Similar Reads
How to Implement Shapeable ImageView in Android?
In Android, ShapeableImageView is used to change the shape of your image to circle, diamond, etc. Also, you can set a corner radius to your ImageView. You can do much more by using this ShapeableImageView with minimal code. So in this article, we are going to make a ShapableImageView in android. By
5 min read
How to Implement Country Code Picker in Android?
Country Code Picker (CCP) is an android library that helps users to select country codes (country phone codes) for telephonic forms. CCP provided a UI component that helps the user to select country codes, country flags, and many more in an android spinner. It gives well-designed looks to forms on t
3 min read
Dynamic Fragment in Android
Dynamic Fragment is a type of fragment that is defined in an XML layout file and called using FragmentManager class. The FragmentManager class is responsible for managing fragments. It is a part of the Activity and its lifecycle depends on the lifecycle of its container activity. Dynamic Fragments a
4 min read
How to Add Memes Using API Call in Android?
Application Programming Interface calling is the process of making requests to external web-based services to retrieve or manipulate data. APIs provide a standardized way for different software applications to communicate with each other. It involves sending a request from one application to another
4 min read
Animation in Android with Example
Animation is the process of adding a motion effect to any view, image, or text. With the help of an animation, you can add motion or can change the shape of a specific view. Animation in Android is generally used to give your UI a rich look and feel. The animations are basically of three types as fo
7 min read
Multimedia Framework in Android
Android multimedia framework is designed to provide a reliable interface for java service. It is a system that includes multimedia applications, frameworks, an OpenCore engine, hardware devices for audio/video/ input, output devices also several core dynamic libraries such as libmedia, libmediaplays
5 min read
Audio Recorder in Android with Example
In Android for recording audio or video, there is a built-in class called MediaRecorder. This class in Android helps to easily record video and audio files. The Android multimedia framework provides built-in support for capturing and encoding common audio and video formats. In android for recording
15+ min read
Android Auto Image Slider with Kotlin
Most e-commerce application uses auto image sliders to display ads and offers within their application. Auto Image Slider is used to display data in the form of slides. In this article, we will take a look at the Implementation of Auto Image Slider in our Android application using Kotlin. A sample v
5 min read
How to Build a QR Code Android App using Firebase?
QR (Quick Response) code is a type of two-dimensional barcode that contains information encoded in a pattern of black and white squares. It was first developed in 1994 by a company named Denso Wave. Qr codes can be scanned by a smartphone or a dedicated QR code scanner, which uses the device's camer
6 min read
How to implement View Shaker in Android
View Shaker is an animation in which, the UI of screen vibrates for a limited period of time. This can be implemented on the whole layout or some particular widget. It is a very common effect that developers use, especially to show incorrect credentials. View Shaker helps us to animate the widgets.
3 min read