1

I have a project requirement like want to store multiple images in room database. so from server side I'm getting image URL so when I'm downloading a image that time only I want to store image in room database and I have total 25 images. After saving images into db, in other fragment want to show these all images for swiping left or right feature.

And for those 25 images, I have one unique id, different name for each image.

First time I'm integrating room database so how can I achieve it? I have gone through many SO answers but didn't get proper idea.

Please guide me. Any help is appreciated.

Menu
  • 677
  • 1
  • 12
  • 30
  • It is unclear where you need help. Can you store one image in room database to begin with? – blackapps Aug 19 '21 at 09:51
  • My question is the same too. You did not answer my question . Please reread my comment and answer to the point. – blackapps Aug 19 '21 at 10:23
  • @blackapps No ...I'm new in this room database implementation. I have no idea that how to store image in db. – Menu Aug 19 '21 at 10:36
  • Well then why dont you just ask how to store one image? Why talking about downloading and multiple images? Forget this post and post a new question. Once you can store one image you can store two too and multiple. And ask specific help. – blackapps Aug 19 '21 at 10:42
  • You should have done a bit of research: https://stackoverflow.com/questions/46337519/how-insert-image-in-room-persistence-library – Kuruchy Aug 19 '21 at 10:53
  • Storing large BLOBs in Android's version of SQLite is not recommended, as Android's API to SQLite does not handle large result sets well. – CommonsWare Aug 19 '21 at 10:56
  • @Kuruchy Thanks for the help...sure I'll try suggested answer. – Menu Aug 19 '21 at 10:57
  • @CommonsWare any other way is available ? – Menu Aug 19 '21 at 11:00
  • Store the images as files on the filesystem, and have your Room table contain an identifier of the file (e.g., a generated filename). – CommonsWare Aug 19 '21 at 11:02
  • @CommonsWare nope not yet and no idea about it...I have just created base classes for room db – Menu Aug 19 '21 at 11:05

3 Answers3

2

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.

Vaibhav Goyal
  • 1,692
  • 1
  • 8
  • 23
  • Pretty bad to convert an image file to a bitmap and then compress the bitmap to a png file in a byte array. If you wanna store byte arrays you better put the image file directly in a byte array. – blackapps Aug 19 '21 at 13:25
1

It is not recommend to store files in SQLite. As you know room works base on SQLite. In your case what i think is to save images in internal storage (It's better to save in file folder because cache folder will be format when ever device storage full) and then save file's name or address in database column and every time you need images you can read address from db and then what ever you want.

Reza Abedi
  • 419
  • 4
  • 16
0

I Have tried to store images in the room database as Vaibhav said. but you will definitely get an Room curser error like this soon or later

Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

In android Must Store images in scoped storage so no need to get any permission and save that image's path in Room database. glide will make your work easy.

Gulab Sagevadiya
  • 872
  • 1
  • 8
  • 20