Ice task 8
RoomDB is a part of Android Jetpack and is designed to provide an abstraction layer over
SQLite, making it easier to work with databases in Android apps.
What type of database is RoomDB?
RoomDB is a SQLite-based object-relational mapping (ORM) database library provided
by Android Jetpack. It helps you manage local data persistence in an Android app by
abstracting away the boilerplate code of raw SQL.
Which language is used to interact with RoomDB data?
You can use Java or Kotlin to interact with RoomDB, although Kotlin is more common in
modern Android development. You interact using:
• Data Access Objects (DAOs)
• Entity classes (annotated data models)
• SQL-like query annotations (@Query, @Insert, etc.)
How to create a RoomDB database in code in an Android app?
Here’s a simple setup using Kotlin:
1. Add dependencies to build.gradle (app level):
implementation "androidx.room:room-runtime:2.6.1"
kapt "androidx.room:room-compiler:2.6.1" // for Kotlin
2. Create an Entity class:
@Entity(tableName = "user_table")
data class User(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
val name: String
)
3. Create a DAO interface:
@Dao
interface UserDao {
@Insert
suspend fun insert(user: User)
@Query("SELECT * FROM user_table")
suspend fun getAllUsers(): List<User>
}
4. Create the Room Database:
@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
@Volatile private var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"app_database"
).build().also { INSTANCE = it }
}
}
}
}
How can a photo be captured?
Use Android’s Intent to open the camera:
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (takePictureIntent.resolveActivity(packageManager) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
To get the full-resolution photo, you need to create a file to save it first using FileProvider.
How can the photo be stored?
You can store the photo in:
• Internal/External Storage as a file (recommended for larger files)
• Or convert it to a Base64 string or ByteArray and store it in RoomDB
(not recommended for large images)
Example to save as a file:
val photoFile = File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"photo.jpg")
val outputUri = FileProvider.getUriForFile(context, "${context.packageName}.provider",
photoFile)
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri)
How can a photo be displayed in an Android app?
You can use an ImageView:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In code, load the photo using:
val imageView: ImageView = findViewById(R.id.imageView)
val bitmap = BitmapFactory.decodeFile(photoFile.absolutePath)
imageView.setImageBitmap(bitmap)
Or use a library like Glide or Picasso:
Glide.with(this).load(photoFile).into(imageView)