You can use this approach if you don't want to deal with Permssions because if user rejects the Permissions, you can't save the images to file folders.
You can't directly save the Image Bitmap in Room Database but you can store it by converting Bitmap to ByteArray and you can use Type Converted Annotation provided by Room Database by using which you can pass the Object in the Format you want but Room Database will store it in the type which it accepts. To create Type Converter, you have to create a new Class Like this,
class Converters {
@TypeConverter
fun fromBitmap(bmp: Bitmap): ByteArray{
val outputStream = ByteArrayOutputStream()
bmp.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
return outputStream.toByteArray()
}
@TypeConverter
fun toBitmap(bytes: ByteArray): Bitmap {
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
}
}
And then add @TypeConverters annotation on Top of Room Database Class ,
@Database(entities = [Entity::class],version = 1 , exportSchema = false)
@TypeConverters(Converters::class)
abstract class Database : RoomDatabase() {
abstract fun getDao(): Dao
}
And Entity class be like,
@Entity(tableName = "running_table")
data class Entity(
var img: Bitmap? = null
)
Now you will pass Bitmap to Entity and then Room Database will convert it to ByteArray and will store it and whenever you want to retrieve the database, you will get Bitmap.