因為每台手機的存取位址不一樣,所以我們不能直接寫死
//val DB_PATH = "/data/data/com.example.last_win/databases" //在手机里存放数据库的位置
//val dbfile = "$DB_PATH/ingerdients.db"
像是上面那樣把路徑給寫死, 手機就會發生閃退的情況 !!
★getDatabasePath>>取得手機裡面有 ingerdients.db 檔案的路徑!!
val db = File(getDatabasePath("ingerdients.db").getPath()) //Get the file name of the databaseval dbdir: File = db.getParentFile()
if (!dbdir.exists()) {
dbdir.mkdirs()
}
Toast.makeText(this, getDatabasePath("ingerdients.db").getPath(), LENGTH_LONG).show()
//执行数据库导入val myInput = applicationContext.assets.open("ingerdients.db") //assets.open("ingerdients.db") //欲导入的数据库val myOutput = FileOutputStream(db)
val buffer = ByteArray(1024)
var length: Int = myInput.read(buffer)
while ((length) > 0) {
myOutput.write(buffer, 0, length)
length = myInput.read(buffer)
}
myOutput.flush()
myInput.close()
myOutput.close()