I'm looking to create开发者_JS百科 a sqlite database on the sd card (don't want to use up the user's internal storage). I'm familiar with the OpenHelper pattern:
public DatabaseFoo(Context context) {
OpenHelper openHelper = new OpenHelper(context);
mDb = openHelper.getWritableDatabase();
}
private static class OpenHelper extends SQLiteOpenHelper {
public OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
...
so if we want to create on the sd card, I think instead we have to use:
public static SQLiteDatabase openOrCreateDatabase (String path,
SQLiteDatabase.CursorFactory factory);
But what is the "factory" argument supposed to be, what factory should be used?
Also a bit worried about what happens if the user removes the SD card while my app is in use..
Thanks
I haven't tried to do what you describe there, but presumably it could be done and might work -- with a few caveats. First, the external storage (SD card) is not secure, so any other application, or the user, could read/write to it. Second, as you noted, when it's unmounted the DB goes away.
Because of these disadvantages, you would probably be better off to try to use an internal storage database (the default), that is small and possibly includes pointers to external data (like images or files) -- that themselves can be on the external storage (and that have placeholders, or other handling, when the external storage is not available).
Still, if you want to try it, you might be better off override the getDatabasePath method of Context, such as with your own Application object, and then pass that into a regular SQLiteOpenHelper. Then you wouldn't have to worry about the cursor factory (which is optional, as the source confirms -- so just pass null if instead you want to go that route).
Do this in your SQLiteOpenHelper constructor:
DatabaseHelper(Context context) {
super(context, context.getExternalFilesDir(null).getAbsolutePath() + "/" + DATABASE_NAME, null, DATABASE_VERSION);
}
It will create the database in the app's folder on the sdcard: /sdcard/Android/data/[your_package_name]/files. In that way the database will be seen as part of the app by android and removed automatically if the user uninstalls the app.
I my app I have a large database and it will in most cases not fit on old phones internal memory, e.g. HTC Desire. It runs great on the sdcard, and most apps are "moved to sdcard" themselves anyway so don't worry about the database not being accessible, because the app won't be accessible it self.
Cursor factory is used to return an instance of your custom Cursor implementation. Generally you just use SQLiteCursor, in which case null is passed as factory argument.
Then make your own flat database - most people have very little internal memory, and its annoying that they eat it up with huge database.
And as for the 'what if they remove the SD' scenario - if the user removes the card obviously its not going to work! Clearly. Just check you didn't get an error when trying to interact with the base, and if you did, just tell you user - problem solved.
public DataBaseHelper(final Context context) {
super(context, Environment.getExternalStorageDirectory()
+ File.separator+ MYDATABASE_NAME, null, MYDATABASE_VERSION);
}
Also Add permission in android Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I would recommend against putting a database onto an SD card - you will significantly decrease the lifespan of the card, since it has a (large, but still existent) limit on the number of writes possible and databases require quite a few writes.
精彩评论