大概的做法是将db文件放置在asset目录或者raw目录下
两者之间的不同处
asset目录
用于存放需要打包到应用程序的静态文件,以便部署到设备中。与res/raw不同点在于,ASSETS支持任意深度的子目录。这些文件不会生成任何资源ID,必须使用/assets开始(不包含它)的相对路径名。
raw目录
存放通用的文件, 该文件夹内的文件将不会被编译成二进制文件,按原样复制到设备上。
主要代码:
private SQLiteDatabase openDatabase(String dbfile) {
// dbfile = DB_PATH + "/" + DB_NAME;
try {
file = new File(dbfile);
if (!file.exists()) {
InputStream is = mContext.getResources().openRawResource(
R.raw.localpush);
FileOutputStream fos = new FileOutputStream(dbfile);
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
fos.flush();
}
fos.close();
is.close();
}
mSQLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
return mSQLiteDatabase;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
}
return null;
}
本来的想法是读取指定的table 拷贝到database中, 后来放弃这种做法!
主要原因是:
1. 操作上比较繁琐
2. 当数据量较大时 是很耗时的