I am getting below error:
not an error (code 0): Could not open the database in read/write mode.
I have added
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.
Your question is not that clear, but I'll try to answer.
Most likely, your database either:
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:
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.
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