http://topic.youkuaiyun.com/u/20110713/08/32b55fd4-cef1-4f8b-ab13-1a3e0a693f50.html
{
public final static String filePath = "/sdcard";
public final static String DB_NAME = "mobilelocation.db";
File file = new File(filePath,DB_NAME);
//File file = new File( "/data/data/com.aiai.www/databases", "attribution.db");
if(copyDB(file)){
mSQLiteDatabase = SQLiteDatabase.openOrCreateDatabase(file, null);
}
}
。。。。
public Boolean copyDB(File file){
try{
if(!file.exists()){ InputStream is = this.getResources().openRawResource(
R.raw.mobilelocation); //欲导入的数据库
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[8*1024];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.flush();
fos.close();
is.close();
}
PS:在ANDROID2.3之前的版本无法复制大于1M文件,需分割复制。
参考http://mobile.51cto.com/abased-337051.htm
//复制assets下的大数据库文件时用这个
private void copyBigDataBase() throws IOException{
InputStream myInput = null;
String outFileName = filePath + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
for (int i = ASSETS_SUFFIX_BEGIN; i < ASSETS_SUFFIX_END+1; i++) {
myInput = this.getAssets().open(DB_NAME + "." + i);
//InputStream ismyInput = this.getResources().openRawResourceFd(DB_NAME + "." + i); //欲导入的数据库
//myInput = myContext.getAssets().open(ASSETS_NAME + "." + i);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myInput.close();
}
myOutput.close();
System.out.println("数据库已经复制");
}