0

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();
    }
EfremovAV
  • 91
  • 10
  • 1
    How are you creating the database? Please, show us your code – Héctor Nov 30 '17 at 11:24
  • Please, see UPD1 – EfremovAV Nov 30 '17 at 11:39
  • Possible duplicate of [android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database](https://stackoverflow.com/questions/17034511/android-database-sqlite-sqlitecantopendatabaseexception-unknown-error-code-14) – Héctor Nov 30 '17 at 11:43
  • 1
    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! – EfremovAV Nov 30 '17 at 11:48

1 Answers1

0

First just try to create that directory on the sdcard. You will see that you wont succeed.

Micro SD cards are read only for modern Android versions.

Except for one app specific directory on it.

Well if you are lucky.

Try to put the database as

/storage/sdcard1/Android/data/<packagename>/files/xAPP/inventory.db
greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Is this the limitation of the SDK? Where can I read about this? – EfremovAV Nov 30 '17 at 12:18
  • Well, you will need `WRITE_EXTERNAL_STORAGE` permission, to use a different folder than the app-specific one? – Christopher Nov 30 '17 at 12:21
  • No. OP will already have that permission as it works for getExternalStorage he said. – greenapps Nov 30 '17 at 12:22
  • Same: Failed to open database '/storage/sdcard1/Android/data/com.xxx.xxx/files/xAPP/inventory.db'. android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database – EfremovAV Nov 30 '17 at 12:25
  • As starting with 4.4 this permission is no longer required for the `getExternalStorage()`: https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE – Christopher Nov 30 '17 at 12:25
  • 1
    Wrong. It is required. It is not required for getExternalFilesDir() only. – greenapps Nov 30 '17 at 12:26
  • I have this permission! – EfremovAV Nov 30 '17 at 12:27
  • Tell us which Android version you use and which device. – greenapps Nov 30 '17 at 12:28
  • Xiaomi Redmi Note 2 with Android 5.0.2 – EfremovAV Nov 30 '17 at 12:29
  • Did you start with all directories already created on the card? Please describe what you did exactly. – greenapps Nov 30 '17 at 12:30
  • I did not quite understand your question, what exactly to describe? – EfremovAV Nov 30 '17 at 12:33
  • Does the directory /storage/sdcard1/Android/data/com.xxx.xxx/files/xAPP now exists on the card? And did you place that db file in it? Or what did you do? – greenapps Nov 30 '17 at 12:34
  • Further you can tell us if getExternalFilesDirs() returns two paths. The second one would be the one we are talking about. – greenapps Nov 30 '17 at 12:39
  • Directory not created. I modified the constructor: private InventoryDBHelper(Context context) { super(context, Settings.getInstance().getSDCardDir() + File.separator + "Android/data/com.xxx.xxx/files/xAPP" + File.separator + DATABASE_NAME, null, DATABASE_VERSION); } – EfremovAV Nov 30 '17 at 12:41
  • `Is this the limitation of the SDK? `. No not of the SDK. Its a 'feature' of modern Android. – greenapps Nov 30 '17 at 12:42
  • I wonder if that db helper would also create not yet existing directories. Are you shure its supposed to do so? Thats why i asked you to create them yourself. – greenapps Nov 30 '17 at 12:44
  • Yes, I want the directory to be created automatically – EfremovAV Nov 30 '17 at 12:46
  • That is no answer to my question. You can want a lot but are you shure that helper will do it normally? And such answers will not help you to solve your problem. Please read my comments again. – greenapps Nov 30 '17 at 12:47
  • `If I put the database to the SD card manually,` I asked you to try this again. Please do. Tell if you had to create directories manually first. – greenapps Nov 30 '17 at 12:51
  • Understood, it works! getExternalFilesDirs () returns 2 directories: /storage/emulated/0/Android/data/com.xxx.xxx/files /storage/sdcard1/Android/data/com.xxx.xxx/files and the second directory is created automatically. Strange why it did not work right away. Thank you! – EfremovAV Nov 30 '17 at 13:39
  • @greenapps, I have to re-open the problem, because this works only in debugging mode, and even not always! – EfremovAV Dec 11 '17 at 10:00
  • Dubugging or not should have no influence. – greenapps Dec 11 '17 at 10:14
  • @greenapps, I do not know how, but at that moment it worked for me. Now the same problem. Yes, it does not work through debugging either.(( – EfremovAV Dec 11 '17 at 10:23
  • @greenapps, There are no options? – EfremovAV Dec 11 '17 at 11:30
  • @greenapps, As I understand, the DBHelper can't create a directory. This is true?! Are there any workarounds? Maybe need to overload some method of the DBHelper? – EfremovAV Dec 11 '17 at 12:36
  • I read, I don't understand why I can't work with my directory: /storage/sdcard1/Android/data//files ? – EfremovAV Dec 11 '17 at 13:00
  • You should elaborate. Android version. Device Make and type. `and even not always! ` You should exactly tell when it works and when not. For testing dont try to create a database. Just try to create a file. A lot of beeing able to write depends on Android version and device. – greenapps Dec 11 '17 at 13:03
  • Xiaomi Redmi Note 2 with Android 5.0.2. I can not create a file in this directory: java.io.IOException: open failed: ENOENT (No such file or directory) at java.io.File.createNewFile(File.java:941) – EfremovAV Dec 11 '17 at 13:27
  • `I can not create a file in this directory` ????? In which directory? Please add a small code block with the code where you try to create a file. Upon a button click for instance. – greenapps Dec 11 '17 at 15:02
  • Works if I get a directory like UPD3 – EfremovAV Dec 11 '17 at 15:04
  • Refresh, once again, please – EfremovAV Dec 11 '17 at 15:06
  • Please look UPD3 in the body of the question – EfremovAV Dec 11 '17 at 20:01
  • I stil dont know which directory you tried. And i also do not know how that code flows in your case. – greenapps Dec 11 '17 at 20:22
  • `UPD3 Works if I get a directory like:` ??? If it works... Then what are you complaining/asking? – greenapps Dec 11 '17 at 20:23
  • @greenapps, I got the directories in a different way, maybe the problem was in this.Thanks for the help and sorry that I distracted you. – EfremovAV Dec 12 '17 at 08:28