Open SQLite database with specified path with Room

Viewed 4380

I am trying to access a databse on an Android's internal storage by specifying its path using the Room library. So far, I have been doing it by using the following statement:

dbPath = "/data/data/my.sqlitetesting/files/database/myDataBase.db";
SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);

How do I acces this DB with Room?

2 Answers

@Jan Malek I use this code to know if I have SD Card and get the path NO SD CARD I then get the path for internal storage

    public void onAvail() {

    String state = Environment.getExternalStorageState();

    if (state.equals(Environment.MEDIA_MOUNTED) && (!state.equals(Environment.MEDIA_MOUNTED_READ_ONLY))) {

        File removable = ContextCompat.getExternalFilesDirs(this, null)[1];
        THE_PATH = String.valueOf(removable) + "/Documents/";

        //System.out.println("ALL TRUE ==> " + THE_PATH);

    } else {// if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        THE_PATH = "";
        //System.out.println("ALL FALSE ==> "+ THE_PATH);
    }
}

Try these few lines of code I believe your path is incorrect in the code you posted if this does not work please advise us

        Context context = this;
    String removable = getFilesDir().getAbsolutePath();
    THE_PATH = String.valueOf(removable)+"/";
    System.out.println("INTERNAL PATH ======> "+THE_PATH);
    File file = new File(context.getFilesDir(), "NAME_OF_FILE");
    String PA = String.valueOf(file);
    System.out.println("Where AM  I ======> "+PA);
Related