I try to create a DB on an SD card, when I call getReadableDatabase () I get
Failed to open database '/storage/sdcard1/xAPP/inventory.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database.
If I put the database to the SD card manually, the following errors appear:
Failed to open database '/storage/sdcard1/xAPP/inventory.db'.
android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
Why? There is no such problem on Internal storage and external storage. Can the problem be in the format of the SD card file system (FAT32)?
P.S. I emphasize that this is not about the external storage, but about the removable storage (SD card)! If I use Environment.getExternalStorageDirectory() all works good!
UPD1 Create Database:
private InventoryDBHelper(Context context) {
super(context, Settings.getInstance().getSDCardDir()
+ File.separator + "xAPP"
+ File.separator + DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// date text yyyy-MM-dd HH:mm:ss
String sql = String
.format("CREATE TABLE %s (_key INTEGER PRIMARY KEY, ProductId TEXT, ProductName TEXT, Price DOUBLE, Cost DOUBLE, DateModified TEXT);",
TABLE_INVENTORY);
db.execSQL(sql);
sql = String
.format("CREATE TABLE %s (ProductId INTEGER, ProductQuantity INTEGER);",
TABLE_QUANTITY);
db.execSQL(sql);
}
UPD2: I have to re-open the problem, because this works only in debugging mode, and even not always!
UPD3 Works if I get a directory like:
File[] dbDirs = ContextCompat.getExternalFilesDirs(context, null);
if (dbDirs[1] != null) {
return dbDirs[1].getAbsolutePath();
} else {
return dbDirs[0].getAbsolutePath();
}