From: http://blog.csdn.net/chthq/article/details/7838033
2. Create a database on the SD card
Through the sqliteopenhelper class source code of Android, you can see the getwritabledatabase of the sqliteopenhelper class
This interface actually calls the openorcreatedatabase method of context, which does not support database names with paths.
That is to say, the database created using this method can only be stored in the/data/package name/directory; To create a database on the SD card
You can call the openorcreatedatabase method of the sqlitedatabase class. This method supports database names with paths.
The following question is how to determine whether an SD card exists and how to obtain the path of the SD card?
Determine whether an SD card exists:
Android. OS. environment. media_mounted.equals (
Android. OS. environment. getexternalstoragestate ());
Obtain the SD card path: String dbpath = Android. OS. environment. getexternalstoragedirectory ()
. Getabsolutepath ();
To create a database on an SD card, follow these steps:
String dbpath = Android. OS. environment. getexternalstoragedirectory ()
. Getabsolutepath () + "/Database ";
File DBP = new file (dbpath );
File DBF = new file (dbpath + "/" + "test. DB ");
If (! DBP. exists ()){
DBP. mkdir ();
}
// Whether the database file is successfully created
Boolean isfilecreatesuccess = false;
If (! DBF. exists ()){
Try {
Isfilecreatesuccess = DBF. createnewfile ();
}
Catch (ioexception ioex ){
}
}
Else {
Isfilecreatesuccess = true;
}
If (isfilecreatesuccess)
DB = sqlitedatabase. openorcreatedatabase (DBF, mfactory );
For convenience, we can use the above method to override the getwritabledatabase method of the sqliteopenhelper class,
For other logics, refer to the sqliteopenhelper class. Finally, do not forget to add the read and write permissions of the SD card:
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>.