Unable to open database in Android Pie (Android 9)

后端 未结 3 1323
日久生厌
日久生厌 2020-12-09 23:01

I used checkDataBase function to ensure if the database already exist to avoid re-copying the file each time you open the application in Oreo. But in Android Pie it is not w

3条回答
  •  感情败类
    2020-12-09 23:34

    I got the solution. In Android Oreo and below version, the way i am accessing db works fine but in Android Pie it wasn't working.This is the way to handle it in Android Pie.

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            MySQLiteOpenHelper helper = new MySQLiteOpenHelper();
            SQLiteDatabase database = helper.getReadableDatabase();
            myPath = database.getPath();
    
        } else {
            String DB_PATH = Environment.getDataDirectory() + "/data/my.trial.app/databases/";
            myPath = DB_PATH + dbName;
        }
    
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        checkDB.disableWriteAheadLogging();
    

提交回复
热议问题