4

Is there a way to open a SQLiteDatabase file from the SD card with OPEN_READWRITE access? This is for external database files provided by a user, not internal application data. (Edit: Handling Uri's to get the actual file paths but hard coded for a simplified example)

The following works using a path to internal phone storage, external to app:

SQLiteDatabase sqlite = SQLiteDatabase.openDatabase(
    "/storage/emulated/0/test.db",
    null,
    SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.NO_LOCALIZED_COLLATORS);

Using a path to the SD card however throws an exception:

SQLiteDatabase sqlite = SQLiteDatabase.openDatabase(
    "/storage/extSdCard/test.db",
    null,
    SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.NO_LOCALIZED_COLLATORS);

Exception:

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

But opening with OPEN_READONLY works fine:

SQLiteDatabase sqlite = SQLiteDatabase.openDatabase(
    "/storage/extSdCard/test.db",
    null,
    SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);

External storage permissions in the manifest:

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

Was hoping the Storage Access Framework or Lollipop SD API could help but am not having any luck.

Is there any way to open a SQLiteDatabase file with write access from the SD card?

Community
  • 1
  • 1
Brian Osborn
  • 49
  • 1
  • 4
  • Use `Environment.getExternalStorageDirectory()` to access the storage, not a fixed path. The SDcard mount point may differ from device to device – Phantômaxx Apr 10 '15 at 13:51
  • I am handling Uri to file paths separately. Just simplified in the question to isolate the problem. – Brian Osborn Apr 10 '15 at 13:54
  • Could this be the source of your problem: http://technofaq.org/posts/2014/04/fixing-external-sd-card-write-issue-on-android-kitkat/ – Raanan Apr 10 '15 at 14:24

1 Answers1

1

Might be related to this: "Android 4.4 KitKat brought in a lot of changes compared to its older 4.3 Jellybean update. And one of the most significant changes is how it handles storage data security.

Prior to Android 4.4 KitKat, applications, provided they had permission to access the external SD card, could read and write to any area on removable storage, including the system folders like DCIM, Alarms, etc. That has all changed, and now third-party applications, as in ones you download from Google Play or elsewhere can only write to files and folders that they have created or have taken ownership of."

http://technofaq.org/posts/2014/04/fixing-external-sd-card-write-issue-on-android-kitkat/

Solution could be copying the file to a local folder before - depends on your usecase.

Raanan
  • 4,777
  • 27
  • 47
  • Thanks for the link. 4.4 did make things difficult. Rooting the phone isn't an option. Was hoping it might be possible on Lollipop at least. I am supporting writable databases in the app memory as well as external on the phone's memory. Also allowing the user to import a copy from the SD card which can then be edited. They can also get read access directly from the SD card. For multiple large databases, write access directly to databases on the SD card would be useful. – Brian Osborn Apr 10 '15 at 20:58