在工程res目录下新建文件夹,命名为raw,把数据库文件(attribution.db)放入该文件夹
new Thread() {
@Override
public void run() {
importAttributionDB();
}
}.start();
private void importAttributionDB() {
final String DB_NAME = "attribution.db"; // 保存的数据库文件名
try {
// 当前程序路径
String appPath = context.getApplicationContext().getFilesDir()
.getAbsolutePath();
String path = appPath.substring(0, appPath.lastIndexOf("/") + 1);
String filename = path + "databases/" + DB_NAME;
if (!(new File(filename).exists())) {// 判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
InputStream is = this.context.getResources().openRawResource(
R.raw.attribution); // 欲导入的数据库
FileOutputStream fos = new FileOutputStream(filename);
byte[] buffer = new byte[1024 * 10];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
}
}
- 1
- 2
前往页