What is TypeConverter in Room? How to use it properly

As Android developers, we should be thankful for the existence of the Room persistence library. Before Room, we had to write tons of lines of code to make SQLite work. It helped us clean those boilerplate codes, as well as making working with SQLite more fun and simple. It provides many functionalities, including conversion between primitive and boxed types, but it doesn’t support object references. Think about the few primitive types that are coming into your mind, such as int, long, float, and char, etc. But sometimes, we need a custom data type whose value you would like to store into a single database column. I’m talking about custom data types such as Date, Bitmap, etc. As I said earlier, this object reference is not possible in Room at the moment. But we don’t need to worry. Here comes TypeConverter for our rescue, which can convert a custom class to and from a known type which Room can persist.

Let’s get our hands dirty.

This is our POJO class called Run,

Look at the first item in the POJO.

It’s a custom object and will not fit into any of the primitive data types we talked about earlier.

And this is our Dao class,

You can see that we are running a query to get all the runs sorted by date, and it’s returning a list of Run, and it will include our custom data type, img, which is a Bitmap. And if you try running this code, you will get the following error message.

error: Cannot figure out how to save this field into database.

You can consider adding a type converter for it.

As the error message suggesting us, let us try creating a Type converter now and see if this error is showing again.

I’m going to create a new class called Converters.

It has two functions.

  1. fromBitmap: This function will take a Bitmap as a parameter and returns a ByteArray. A ByteArray is nothing but an array of bytes. As you have correctly guessed, a byte is a primitive type which is known for Room.
  2. toBitmap: This function will take a ByteArray as a parameter and returns a Bitmap. As you have correctly guessed again, it is our custom object in this case.

Also note that we have used an annotation called, “@TypeConverter” before the starting of each function.

Next, we have to make a small change in our database class.

Take a look into this line,

We have added the “@TypeConverters” annotation to our database class so that Room can use the converter we have defined earlier to convert our Bitmap to ByteArray and vice versa. You can put as many converters as you want.

Now try running that query again and see if there are any more errors popping up.

If you have followed the above steps correctly, it will work perfectly. And congrats. You have learned how to use TypeConverter. For more information about TypeConverter in Room, please check out the official documentation from the Android developers. I have learned more about TypeConverter by watching this guy’s(Philipp Lackner) YouTube channel. He talks in-depth about Android development and Kotlin.

Lastly, if you liked reading this post and genuinely thinking it helped you, please give a clap and share it among your fellow developer friends. Thank you. Be safe.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store