Unable to open database in Android Pie (Android 9)

感情迁移 提交于 2019-11-30 14:56:18

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();
Mansoor Ali
context.getDatabasePath(the_database_name).getPath();

Works fine since getDatabasePath() returns the absolute path on the filesystem where a database created.

Override following method on SQLiteOpenHelper class for android PIE OS version.

@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        db.disableWriteAheadLogging();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!