SQLiteAssetHelper - problems on specific phones, e.g. OnePlus

坚强是说给别人听的谎言 提交于 2019-12-22 05:37:16

问题


I'm experiencing crashes on some devices when using SQLiteAssetHelper in my app, most of all on OnePlus devices. Now I read here that it has to do with the directory the database is stored.

Now I'm trying to find a workaround, the best I could come up with currently is a constructor like this

public MySubclass(Context context) {
    super(context, databaseName, context.getFilesDir() + "/databases", null, databaseVersion);

Is this the correct way to do it or are there other problems with this approach?

EDIT

The exception is

Fatal Exception: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version x to y: /data/data/my.package.id/databases/database.db

Sorry, I linked the wrong SO-question: this is the correct one. There it says 'OnePlus is able to copy the database to /data/data/package-name/databases/filename.db but it doesn't allow to access that data and i have no clue regarding that.'


回答1:


The SQLiteAssetHelper constructor allows you to specify your own database destination path (the folder needs to be writable). If you don't specify any it is using one by default. See this excerpt from the Github repo:

    if (storageDirectory != null) {
        mDatabasePath = storageDirectory;
    } else {
        mDatabasePath = context.getApplicationInfo().dataDir + "/databases";
    }



回答2:


Hey you can copy database from one to another from below mentioned code.

public class MySQLiteHelper extends SQLiteOpenHelper implements DBConstants {

private static MySQLiteHelper mInstance = null;
private SQLiteDatabase myDataBase;
private static String DB_PATH = "";

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    try {
        if (checkDataBase())
            openDataBase();
        else
            myDataBase = this.getReadableDatabase();
    } catch (Exception e) {
    }
}

public static MySQLiteHelper instance(Context context) {

    File outFile = context.getDatabasePath(DATABASE_NAME);
    DB_PATH = outFile.getPath();

    if (mInstance == null) {
        mInstance = new MySQLiteHelper(context);
    }

    return mInstance;
}


来源:https://stackoverflow.com/questions/34068268/sqliteassethelper-problems-on-specific-phones-e-g-oneplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!