In fact, it is very simple, is to put our database files on our mobile phone, so do not have to limit where to write this code, the first time you create a database, I think in the Software start Page effect better, first of all we should be written in advance database files such as Test.db are placed in the raw folder in the Res folder or in the assets, because these folders will not be compressed when the APK is generated.
1,databaseutil is used to copy the db file from raw to the phone with the following code
Copy Code code as follows:
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import android.database.sqlite.SQLiteException;
Import COM.ATA.APP.R;
/**
* Copy Database to APK package
*
* @author NGJ
*
*/
public class Databaseutil {
private context;
public static String dbname = "kao.db";//the name of the database
private static String database_path;//the path of the database on the phone
Public Databaseutil {
This.context = context;
String PackageName = Context.getpackagename ();
Database_path= "/data/data/" +packagename+ "/databases/";
}
/**
* Determine if the database exists
*
* @return False or True
*/
public Boolean checkdatabase () {
Sqlitedatabase db = null;
try {
String databasefilename = Database_path + dbname;
db = Sqlitedatabase.opendatabase (DatabaseFileName, null,sqlitedatabase.open_readonly);
catch (Sqliteexception e) {
}
if (db!= null) {
db.close ();
 &NBSP}
return db!= null? true:false;
.}
/**
* Copy the database to the mobile phone specified folder
*
* @throws IOException
*/
public void Copydatabase () throws IOException {
String databasefilenames = Database_path + dbname;
File dir = new file (Database_path);
if (!dir.exists ())//To determine whether the folder exists, do not exist to create a new
Dir.mkdir ();
FileOutputStream OS = new FileOutputStream (databasefilenames);//Get write stream of database file
InputStream is = Context.getresources (). Openrawresource (R.raw.kao);//Get Data stream of database file
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read (buffer)) > 0) {
Os.write (buffer, 0, count);
Os.flush ();
}
Is.close ();
Os.close ();
}
}
2, add the following method to the required activity for the specific copy operation
Copy Code code as follows:
Java code
Privatevoid Copydatabasetophone () {
Databaseutil util = new Databaseutil (this);
Determine if the database exists
Boolean dbexist = Util.checkdatabase ();
if (dbexist) {
LOG.I ("tag", "The database is exist.");
else {//does not exist the database in Raw is written to the phone
try {
Util.copydatabase ();
catch (IOException e) {
Thrownew error ("Error copying database");
}
}
}
3, detect whether there are sdcard, perform copy. (personal feeling can not detect whether the SD card exists, but do not detect seems to have a problem, program reasons?) )
Copy Code code as follows:
Boolean hassdcard = Environment.getexternalstoragestate (). Equals (environment.media_mounted);
if (Hassdcard) {
Copydatabasetophone ();
}else{
Showtoast ("SDcard not Detected");
}