We can copy database from assets folde by class CopyAssetDatabaseOpenHelper(private val context: Context) { companion object { private val DB_NAME = "dbName.db" fun openDatabase(): SQLiteDatabase { checkDB?.close() } val buffer = ByteArray(1024) currentDB.flush() }
}
val dbFile = context.getDatabasePath(DB_NAME)
if (!dbFile.exists()) {
try {
val checkDB = context.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE,null)
copyDatabase(dbFile)
} catch (e: IOException) {
throw RuntimeException("Error creating source database", e)
}
return SQLiteDatabase.openDatabase(dbFile.path, null, SQLiteDatabase.OPEN_READWRITE)
}
private fun copyDatabase(context:Context) {
val dbFile = context.getDatabasePath("logicaldb")
val assetdb = context.assets.open("logicaldb")
val currentDB = FileOutputStream(dbFile)
while (assetdb.read(buffer) > 0) {
currentDB.write(buffer)
Log.d("#DB", "Copying "+buffer.toString())
}
currentDB.close()
assetdb.close()
}
What is Application?
How to get an image resource by it's name in android?
Extract Date and Time from Long Type Variable - RRutors
How do I generate random numbers in Dart?
Fix Play Store Rejection Due to SMS Permission - RRutors
What is ANR? How to avoid and resolve it?
What is the difference between match_parent and fill_parent?
Fix Duplicate Files in APK Build.gradle Issue - RRutors
What is Android?
How do i load image from drawable folder in Jetpack Compose?
Android Studio project R can't find
How to copy database from assets folder in android using kotlin
What are the notifications available in android?
FragmentPagerAdapter deprecated, Since API 27.1.0 FragmentPagerAdapter is deprecated.
How to install/ uninstall apk by command line ADB
How to Get bitmap from drawable: Android
What is Android Fragments?
FCM Not Receiving Notifications in Android - RRutors
Set Activity to Fullscreen Mode in Android - RRutors
What happens next when the device switches between portrait and landscape?
How to start a new activity on button click
How to convert DP to Pixel and Pixel to Dp?
Symmetric vs Asymmetric Encryption in Android Server - RRutors
Fix Behavior Subclass BottomSheetBehavior Exception - RRutors
What is the Different Between val and var in Kotlin?
Add Jar/Library Files as Dependency in Android Studio
What is "shared preferences" in Android ?
How to find Android Device UDID or unique ID?
What are Android Versions available?
Open PDF in Google Drive with Intent in Android - RRutors
ERR_ACCESS_DENIED in Android Webview with sdk 30
How do I check in SQLite whether a table exists?
Permission denied for window type 2010 in Marshmallow device
Fix Android Emulator Kill Error When Running Your App
What is Intent? What is the difference between an implicit intent and an explicit intent?
INSTALL_FAILED_INVALID_APK: Split lib_slice_9_apk was defined multiple times. It is possible that th
Explain Fragment Life Cycle
How to avoid multiple button click at same time in android?
Decompile APK Files: How to Get Source Code - RRutors Guide
How to Exit android app on back pressed?