SQLiteAssetHelper - problems on specific phones, e

2019-04-28 07:07发布

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.'

2条回答
在下西门庆
2楼-- · 2019-04-28 07:17

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";
    }
查看更多
我想做一个坏孩纸
3楼-- · 2019-04-28 07:31
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;
}
查看更多
登录 后发表回答