我目前正尝试使用. createFromAsset从资产内的数据库文件预填充ROOM数据库。在运行应用程序时,我收到"原因:java.lang.RuntimeException:无法复制数据库文件。"错误。
我的数据库文件路径assets/数据库/cardb.db
E/AndroidRuntime: FATAL EXCEPTION: arch_disk_io_0
Process: com.example.myapplication, PID: 8068
java.lang.RuntimeException: Exception while computing database live data.
at androidx.room.RoomTrackingLiveData.refreshRunnable$lambda-0(RoomTrackingLiveData.kt:74)
at androidx.room.RoomTrackingLiveData.$r8$lambda$UkyPj-RMUoTXOMbUuy5NWSwmo0E(Unknown Source:0)
at androidx.room.RoomTrackingLiveData$$ExternalSyntheticLambda0.run(Unknown Source:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)
Caused by: java.lang.RuntimeException: Unable to copy database file.
at androidx.room.SQLiteCopyOpenHelper.verifyDatabaseFile(SQLiteCopyOpenHelper.kt:114)
at androidx.room.SQLiteCopyOpenHelper.getWritableDatabase(SQLiteCopyOpenHelper.kt:68)
at androidx.room.RoomDatabase.inTransaction(RoomDatabase.kt:629)
at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.kt:448)
at androidx.room.RoomDatabase.query(RoomDatabase.kt:477)
at androidx.room.util.DBUtil.query(DBUtil.kt:75)
at com.example.myapplication.data.CarDao_Impl$1.call(CarDao_Impl.java:34)
at com.example.myapplication.data.CarDao_Impl$1.call(CarDao_Impl.java:31)
at androidx.room.RoomTrackingLiveData.refreshRunnable$lambda-0(RoomTrackingLiveData.kt:72)
at androidx.room.RoomTrackingLiveData.$r8$lambda$UkyPj-RMUoTXOMbUuy5NWSwmo0E(Unknown Source:0)
at androidx.room.RoomTrackingLiveData$$ExternalSyntheticLambda0.run(Unknown Source:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)
Caused by: java.io.FileNotFoundException: database/cardb.db
at android.content.res.AssetManager.nativeOpenAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:904)
at android.content.res.AssetManager.open(AssetManager.java:881)
at androidx.room.SQLiteCopyOpenHelper.copyDatabaseFile(SQLiteCopyOpenHelper.kt:156)
at androidx.room.SQLiteCopyOpenHelper.verifyDatabaseFile(SQLiteCopyOpenHelper.kt:111)
at androidx.room.SQLiteCopyOpenHelper.getWritableDatabase(SQLiteCopyOpenHelper.kt:68)
at androidx.room.RoomDatabase.inTransaction(RoomDatabase.kt:629)
at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.kt:448)
at androidx.room.RoomDatabase.query(RoomDatabase.kt:477)
at androidx.room.util.DBUtil.query(DBUtil.kt:75)
at com.example.myapplication.data.CarDao_Impl$1.call(CarDao_Impl.java:34)
at com.example.myapplication.data.CarDao_Impl$1.call(CarDao_Impl.java:31)
at androidx.room.RoomTrackingLiveData.refreshRunnable$lambda-0(RoomTrackingLiveData.kt:72)
at androidx.room.RoomTrackingLiveData.$r8$lambda$UkyPj-RMUoTXOMbUuy5NWSwmo0E(Unknown Source:0)
at androidx.room.RoomTrackingLiveData$$ExternalSyntheticLambda0.run(Unknown Source:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)
这是我的数据库文件
package com.example.myapplication.data
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import java.io.File
@Database(entities = [CarMake::class], version = 1, exportSchema = false)
abstract class CarDatabase: RoomDatabase() {
abstract fun carDao(): CarDao
companion object{
@Volatile
private var INSTANCE : CarDatabase? = null
fun provideDatabase(context : Context): CarDatabase{
//Log.d("MyActivity", context.toString())
val tempInstance = INSTANCE
if (tempInstance != null){
return tempInstance
}
synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext,
CarDatabase::class.java,"car_database")
.createFromAsset("database/cardb.db")
.build()
INSTANCE = instance
return instance
}
}
}
}
我试着重新创建数据库文件以防出错。我可以在SQLite浏览器中打开它并查看结构和数据。我还试着重命名数据库文件,将db移动到assets文件夹中。此外,我尝试在虚拟设备中查看我的应用程序数据,但数据库不存在于资产文件夹中。我希望数据库只需简单地复制,并按其预期的方式运行。
我看过一些关于这个问题的帖子。
1条答案
按热度按时间yhuiod9q1#
根据您的屏幕截图,您的
assets/
目录位于androidTest
源集中,需要将其移动到main
源集中。