0

Try to load data from a local db file (preferably in SD Card).

I first check if sd-card is mounted before performing any operation.

    if (isExternalStorageWritable()) {
        loadFile(SharedPref.getDBPath(this), new DataLoaderCallback() {
            @Override
            public void onDataRetrieved(ImportModel importModel) {

            }
        });
    }

this basically calls this part of the code that crashses the app.

private SQLiteDatabase connect() {
        String dbPath = SharedPref.getManifestPath(mContext);
        File dbfile = new File(dbPath);
        if (dbfile.exists()) {

            return SQLiteDatabase.openDatabase(dbfile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);
        }
        return null;
    }

and this is the error :

Failed to open database '/storage/sdcard1/Alarms/data/manifest'.
                                                                    android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
                                                                        at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
                                                                        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:212)
                                                                        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:196)
                                                                        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
                                                                        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
                                                                        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
                                                                        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
                                                                        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
                                                                        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
                                                                        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)

When I use:

  return SQLiteDatabase.openDatabase(dbfile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);

It does work but I can only read from the file. I want to read and write. Hence, OPEN_READWRITE . The file has read and write permission , and I have this permissions added in manifest.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

How do I fix this ?

Belvi Nosakhare
  • 3,107
  • 5
  • 32
  • 65
  • Don't you have to copy the database into private app storage before you can write to it? – OneCricketeer Nov 07 '16 at 13:02
  • writing to database on sdcard needs few things to check ,as stated in several posts, like: your app is the owner of the file, storage is mounted r/w, file is not read-only. have alook here http://stackoverflow.com/questions/37593384/not-able-to-open-database-in-read-write-mode and here http://stackoverflow.com/questions/30082008/attempt-to-write-a-readonly-database-but-im-not – Yazan Nov 07 '16 at 13:17

0 Answers0