这个可以在每次应用开启时检测database文件夹下是否有这个名称的数据库,如果没有就从asserts中获得释放一次,给你看一下我的代码:
我的asserts里有一个文件叫做:address.db
/**
*在onCreate()方法中调用
*/
private File initDatabase (){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File file = getDatabasePath("address.db");
byte [] buffer = new byte[1024];
int c;
try {
if (!file.exists())
file.getParentFile().mkdirs();
else
return file;
bis = new BufferedInputStream(getAssets().open("address.db"));
bos = new BufferedOutputStream(new FileOutputStream(file));
while ((c = bis.read(buffer)) != -1)
bos.write(buffer, 0, c);
} catch (IOException e) {
e.printStackTrace();
return null;
}finally{
try {
if (bis != null)
bis.close();
if (bos != null) {
bos.close();
bos.flush();
}
}catch (IOException e){
e.printStackTrace();
}
}
return file;
}