9

I am getting below error:

not an error (code 0): Could not open the database in read/write mode.

I have added

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

I am trying to enter data in the database that is present in the SD card, I am able to read the data that is already present in the database through "OPEN_READONLY". I am getting error when using "OPEN_READWRITE"

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139

4 Answers4

3

Your question is not that clear, but I'll try to answer.

Most likely, your database either:

  1. Don't exist yet, and you have to create it;
  2. Your database file is read only, you have to change it (This question might be related).

For #2, instead of using SQLiteOpenHelper#getReadableDatabase(), use SQLiteOpenHelper#getWritableDatabase


If your database is on an external storage unit, you have a few other things to check:

  1. Is the external storage currently mounted? If not, you can't access the db.
  2. Is it mounted as read only? If so, you'll have to change this.
  3. Have you checked the path? Is it correct?

The problem might be on any of those topics.

To check if it's mounted as read-only, try the following:

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

Taken from here.

For more info on the Environment class, please refer to the docs.

Community
  • 1
  • 1
Mauker
  • 11,237
  • 7
  • 58
  • 76
1

In Android API 4.3 and lower you can open databases in external SD Card, this code was working for read and write :

SQLiteDatabase.openDatabase(DB_PATH, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);

Starting from API 4.4 you only can open databases in external SD Card READ_ONLY, you have to use this code :

SQLiteDatabase.openDatabase(DB_PATH, null,SQLiteDatabase.OPEN_READONLY);

I hope this helps.

Wowo Ot
  • 1,362
  • 13
  • 21
0

With version 4.4 KitKat some changes were introduced related to how Android handles storage and data security.

For example even if isExternalStorageReadable() will return true you will not be able to write to External SD Card but only to the directory returned by Environment.getExternalStorageDirectory().

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

To be able to delete, write, move files you have to use the Storage Access Framework which lets you access files located on your phone but also any remote files like for example files in Google Drive.

To open a database you have to provide a path but the Document Provider Api only lets you open an Input Stream or move files so in order to open a database you will have to move/copy the file to some Private Internal storage or to Environment.getExternalStorageDirectory() directory.

More info on this topic:

https://developer.android.com/guide/topics/providers/document-provider.html

https://stackoverflow.com/a/43317703/1502079

https://stackoverflow.com/a/21674485/1502079

vovahost
  • 34,185
  • 17
  • 113
  • 116
0

Answer for year 2022: In case you are copying the database file after installation of the app and see this error:

not an error (code 0): Could not open the database in read/write mode.

Just copy the database file (from your pc) when app is running.

Or alternatively set the folder permission as 777 by adb shell after the copy is finished.

Ali Has
  • 598
  • 1
  • 8
  • 24