1

In our project we need to store databases to an external SD-card instead of the default path. To do this we have a custom class that inherits from ContextWrapper and overrides openOrCreateDatabase:

@Override
public SQLiteDatabase openOrCreateDatabase(String name, 
                                           int mode, 
                                           SQLiteDatabase.CursorFactory factory) { 
    ...
}

In API level 11 a new version of openOrCreateDatabase was added that adds a parameter of type DatabaseErrorHandler. On ICS it seems like it's this version that is called. I have verified this using:

@Override
public SQLiteDatabase openOrCreateDatabase(String name, 
                                           int mode, 
                                           SQLiteDatabase.CursorFactory factory,
                                           DatabaseErrorHandler errorHandler) {
    return openOrCreateDatabase(name, mode, factory);
}

The problem is that I cannot add this function to the code because DatabaseErrorHandler was added in API level 11 and I need to have backwards compatibility with at least API level 8.

So how to I solve this issue?

tidbeck
  • 2,363
  • 24
  • 35
  • Is there a reason why you aren't just using the static `openOrCreateDatabase()` methods on `SQLiteDatabase` directly? – CommonsWare Mar 13 '12 at 19:39
  • Because I'm using `SqliteOpenHelper`. I've implemented `SqliteOpenHelper` sub-classes to handle my databases and passing my `ContextWrapper` as argument. The call to `openOrCreateDatabase` is made from the super-class (e.g. `SqliteOpenHelper`). – tidbeck Mar 13 '12 at 23:21
  • `SQLiteOpenHelper` has supported databases on external storage for a while now -- 2.2, maybe? There was no documented change IIRC, so I forget exactly when the shift came about, but depending on what versions of Android you're supporting, `SQLiteOpenHelper` may work without changes. – CommonsWare Mar 13 '12 at 23:23
  • By sending in the full path to the super constructor from my `SQLiteOpenHelper` subclass? If so, the only requirement is that I know the full path before creating the subclass? – tidbeck Mar 14 '12 at 09:28
  • Yes and yes. It used to be that `SQLiteOpenHelper` only accepted a name, but I'm fairly certain they extended it to support a fully-qualified path. – CommonsWare Mar 14 '12 at 09:50
  • Ok, thank you. Could you maybe add add an answer that I can accept? – tidbeck Mar 14 '12 at 10:01

1 Answers1

1

AFAIK, as of Android 2.2, SQLiteOpenHelper supports databases on external storage, by supplying a full path to the database file.

That being said, bear in mind that external storage and internal storage sorta merged with Android 3.0. In Android 1.x and 2.x, internal and external storage were separate partitions with separate space. In Android 3.0+, they share a partition, with external storage simply being a designated directory in the partition used by internal storage.

Hence, if the reason you are using external storage is due to size considerations, that will no longer be needed starting with API Level 11. If you are using external storage specifically because the user has access to it, that would still be relevant, though I've always been nervous about that, and am more nervous now because Android 3.0 now allows simultaneous access to external storage by apps and the user.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • The reason is that the solution uses 'special' SD-cards that all data needs to be put on, to be able to move it to another device etc. – tidbeck Mar 14 '12 at 10:46