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()
}
Explain Fragment Life Cycle
What is ANR? How to avoid and resolve it?
What is Intent? What is the difference between an implicit intent and an explicit intent?
How to get a contact image using a phone number in android?
How to get an image resource by it's name in android?
How to check Android version?
What happens next when the device switches between portrait and landscape?
What is NetworkOnMainThread Exception?
How to validate email address in Android with Kotlin?
Android Activity FullScreen - How to set activity to fullscreen mode in Android?
Android FCM not receiving notifications when app is removed from background
What is Context?
How to install/ uninstall apk by command line ADB
How To Set Text Color Programmatically Android TextView?
What is Android?
Hide/Disable soft keyboard on Activity launch: Android
ERROR Android emulator gets killed when I try to run my application and choose an emulator it ret
How to get screen size (width,height) in Android?
How to Generate signed apk with android studio
Key Points to remember while develop the Android Application.
'constructor Handler()' is deprecated. Deprecated in Java, How to use Handler() class in And
Error: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path
What is Android Fragments?
What is Application?
Could not inflate Behavior subclass android.support.design.widget. BottomSheetBehavior How resolve
What are the notifications available in android?
What are Android Versions available?
How to find Android Device UDID or unique ID?
How can i comment inside xml file in android studio?
How to save activity State in Android?
How to reduce APK package size?
How to convert DP to Pixel and Pixel to Dp?
How to copy database from assets folder in android using kotlin
Duplicate files during packaging of APK” build.gradle issue: Android Studio
How to Exit android app on back pressed?
Intent for Drive PDF Viewer - How to open PDF in Google Drive with intent
What is the Different Between val and var in Kotlin?
What is "shared preferences" in Android ?
What are symmetric and asymmetric encryption in the way Android interacts with the server?
How to add jar/library files as a dependency in Android studio gradle file