Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.RuntimeException: Unable to copy database file. occuring when trying to pre-populate a ROOM database

I am trying to Pre-Populate my Android ROOM with a database2.db file which I copied over from my device by making the app generate the Database Values pragmatically. Which worked when I initially Installed the app however upon Uninstalling and Reinstalling to force a ROOM database build I get the following Error

E/AndroidRuntime: FATAL EXCEPTION: arch_disk_io_0
Process: com.Adictya.timely, PID: 16244
java.lang.RuntimeException: Exception while computing database live data.
    at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:919)
 Caused by: java.lang.RuntimeException: Unable to copy database file.
    at androidx.room.SQLiteCopyOpenHelper.verifyDatabaseFile(SQLiteCopyOpenHelper.java:131)
    .
    .
    .
    .
 Caused by: java.io.FileNotFoundException: database/database2.db
    at com.Adictya.timely.data.TimeSlotsDAO_Impl$7.call(TimeSlotsDAO_Impl.java:313) 
    .
    .
    .

The App works fine once I remove the .createFromAsset("database/database2.db)` and I have ensured the file is in assets/database package.

Here is my RoomDatabase Class

@Database(entities = {Classes.class, TimeSlots.class}, version = 1, exportSchema = false)
public abstract class TimeSlotsRoomDatabase extends RoomDatabase {

    public static volatile TimeSlotsRoomDatabase INSTANCE;
    public abstract ClassesDAO classesDAO();
    public abstract TimeSlotsDAO timeSlotsDAO();

    public static TimeSlotsRoomDatabase getDatabase(final Context context){
        if (INSTANCE == null){
            synchronized (TimeSlotsRoomDatabase.class){
                if (INSTANCE == null){
                    //create our db
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),TimeSlotsRoomDatabase.class,"timeslots_db")
                            .addCallback(roomDatabaseCallback).createFromAsset("database/database2.db").fallbackToDestructiveMigration().build();
//                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),TimeSlotsRoomDatabase.class,"timeslots_db")
//                            .addCallback(roomDatabaseCallback).build();
                }
            }
        }
        return INSTANCE;
    }
    public static RoomDatabase.Callback roomDatabaseCallback = new RoomDatabase.Callback() {
        @Override
        public void onOpen(@NonNull SupportSQLiteDatabase db){
            super.onOpen(db);
            new PopulateDbAsync(INSTANCE).execute();
        }
    };

    private static class PopulateDbAsync extends AsyncTask<Void, Void, Void>{
        private final TimeSlotsDAO timeSlotsDAO;
        private final ClassesDAO classesDAO;
        public PopulateDbAsync(TimeSlotsRoomDatabase db){
            timeSlotsDAO = db.timeSlotsDAO();
            classesDAO = db.classesDAO();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            timeSlotsDAO.deleteAll();
            classesDAO.deleteAll();
            //for testing
            Classes classes = new Classes("CSE1002","Microprocessor and Interfacing","Micro");
            classes = new Classes("CSE1003","Internet of Things","IOT");

            TimeSlots timeSlots = new TimeSlots("A1",0,0,"8:00 AM","Microprocessor and Interfacing","SJT-522");
            timeSlotsDAO.insert(timeSlots);

        }
    }
}
like image 641
Adictya Avatar asked Oct 24 '25 05:10

Adictya


1 Answers

I made a silly mistake and place the assets folder inside my main/java directory. It should be as CommonsWare pointed out

as a sub-directory of src/, making it a peer of java/, res/, etc.

Thanks again for your help.

like image 85
Adictya Avatar answered Oct 26 '25 21:10

Adictya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!