android implements the raw folder to import the database code
- 2020-05-19 05:48:25
- OfStack
Here's an interview question:
How to publish SQLite database (dictionary.db file) with apk file 1?
A: just put this file in the /res/raw directory. The files in the res\raw directory will not be compressed, so the files in the directory can be extracted directly, and the resource id will be generated.
So how do you import the database below the raw file into the database directory in your installed program?
public void imporDatabase() {
// The directory where the database resides
String dirPath="/data/data/com.hkx.wan/databases";
File dir = new File(dirPath);
if(!dir.exists()) {
dir.mkdir();
}
// Database file
File file = new File(dir, "abc.db");
try {
if(!file.exists()) {
file.createNewFile();
}
// Load the database to be imported
InputStream is = this.getApplicationContext().getResources().openRawResource(R.raw.db_weather);
FileOutputStream fos = new FileOutputStream(file);
byte[] buffere=new byte[is.available()];
is.read(buffere);
fos.write(buffere);
is.close();
fos.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}