简体   繁体   中英

Unable to Backup my database file in Storage directory. Using Room

I am trying to back up my database file in to the storage directory. But as soon as I click the save button the application crashes. Saying it cannot open the database.

This the DB helper class.

public abstract class DBHelper extends RoomDatabase {

    private static final String DB_NAME = "MyDatabase";
    private static String DB_PATH = "/storage/emulated/0/documents/Expenses.db";

    private static DBHelper instance;
   // public static final String DB_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/appName/database/" ;

    public static synchronized DBHelper getDB(Context context) {
        if (instance == null) {
                instance = Room.databaseBuilder(context, DBHelper.class, DB_PATH + DB_NAME)
                        .fallbackToDestructiveMigration()
                        .allowMainThreadQueries()
                        .setJournalMode(JournalMode.TRUNCATE)
                        .build();


            }
        return instance;
    }
    public abstract ExpenseDao expenseDao();

}

I have given all the permissions in the manifest files. So that is not an issue.

Logcat error


2023-01-30 12:11:17.972 10418-10418/com.example.roomlibrary E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.roomlibrary, PID: 10418
    android.database.sqlite.SQLiteCantOpenDatabaseException: Cannot open database '/storage/emulated/0/Documentsmnt/sdcard/documents/expenses.dbMyDatabase': Directory /storage/emulated/0/Documentsmnt/sdcard/documents doesn't exist
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:254)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:205)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:505)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:198)


Is /storage/emulated/0/Documentsmnt/sdcard/documents really expected to exist?

Should it not be /storage/emulated/0/documents ie it appears that you may be inadvertently concatenating mnt/sdcard/documents

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM