How to store Bitmap into Room Database using Base64 Encoding

Deepak K C
Bobble Engineering
Published in
3 min readDec 13, 2022

--

The Room databases provide an abstraction layer on top of SQLite to make storing the data in databases easier.

Key Benefits of Room Database:

1. Compile-time verification of SQL queries.

2. Convenience annotations that minimize the boilerplate code. [ Earlier, we had to write a lot of boilerplate code such as to create tables, database connectivity, and SQL queries to perform operations on data in the databases.

When we want to store non-primitive data types in Room Database. There this problem arrives in that it doesn’t support storing class objects. We need to convert the objects into some other data type that is supported by Room Database, such as String, Base64, ByteArray, etc.

I will describe how to store Bitmap in Room Database using the annotations provided by Room Library.Steps to convert bitmap to base64 and reverse

//DataItemClass.kt <-file name// Each instance of data class annotated with @Entity represents a row in a table in the database.@Entity(tableName = "data_items")
data class DataItemClass(
@PrimaryKey(autoGenerate = true)//Generates unique values for id
val id:Int = 0,
val name:ByteArray,
val phone:String,
val address:String,
val bitmap:Bitmap
)

The above code represents a table named data_items in the database with columns id, name, phone, address, bitmap.

Now, when we try to store this directly into database. It throws an error:

Now, we need to use type converters for the bitmap.

When it comes to storing non-primitive types into the Room Database, TypeConverter annotation is very helpful to us.

Steps to convert Bitmap to Base64 string and reverse.

TypeConverter : This annotation is used to define the type converters for the data, we want to store.

There are 2 methods for this task bitmapToBase64() and base64ToBitmap()

Annotate these methods with TypeConverter, whichwill be utilised to convert bitmap to base64 and reverse.


class Converters{
@TypeConverter
fun bitmapToBase64(bitmap: Bitmap) : String{
// create a ByteBuffer and allocate size equal to bytes in the bitmap
val byteBuffer = ByteBuffer.allocate(bitmap.height * bitmap.rowBytes)
//copy all the pixels from bitmap to byteBuffer
bitmap.copyPixelsToBuffer(byteBuffer)
//convert byte buffer into byteArray
val byteArray = byteBuffer.array()
//convert byteArray to Base64 String with default flags
return Base64.encodeToString(byteArray, DEFAULT)
}

@TypeConverter
fun base64ToBitmap(base64String: String):Bitmap{
//convert Base64 String into byteArray
val byteArray = Base64.decode(base64String, DEFAULT)
//byteArray to Bitmap
return BitmapFactory.decodeByteArray(byteArray,
0, byteArray.size)
}
}

Generally, we annotate the Database class with TypeConverters to include the type converter methods which are defined in the Converters class.

Note: @TypeConverters can be used in different scopes.

1. If we use it on the Database it can be accessed by all the DAOs and Entities contained in it.

2. If we use it on DAO it can be all the methods defined in DAO.

@TypeConverters(Converters::class)
@Database(entities = [DataItemClass::class], version = 1)
abstract class DatabaseData:RoomDatabase(){
/Database-related code
}

In the above example, We are annotating the Database class with @TypeConverters and passed the Converters class, in which we have defined converter methods.

--

--