Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 881
swalkner Avatar asked Dec 03 '15 14:12

swalkner


2 Answers

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";
    }
like image 180
narko Avatar answered Sep 30 '22 12:09

narko


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;
}
like image 26
Amit Sinha Avatar answered Sep 30 '22 10:09

Amit Sinha