Home > Mobile >  Pre-Populating ROOM Database results in "java.lang.RuntimeException: Unable to copy database fi
Pre-Populating ROOM Database results in "java.lang.RuntimeException: Unable to copy database fi

Time:01-12

I'm currently trying to pre-populate a ROOM database from a database file within assets using .createFromAsset. While running my application, I get "Caused by: java.lang.RuntimeException: Unable to copy database file." error.

My db file path assets/database/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) 

Directories

Here is my database file

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
            }

        }

    }
}

I tried recreating the database file in case I did something wrong. I am able to open it up and see the structure and data within an SQLite browser. I also tried renaming the database file, moving the db just inside of the assets folder. Furthermore, I tried looking into my app data within the virtual device and the database is not present in the assets folder. I expect for the database to simply be copied and function the way it is intended to.

I've read some posts regarding this issue.

CodePudding user response:

According to your screenshot, your assets/ directory is in the androidTest source set. That needs to be moved to the main source set.

  • Related