Add the external database example of android external database to the installation
- 2020-05-27 07:11:59
- OfStack
When android was packaged and installed, it added external database. I had this requirement, so I wrote the following code, which I will share with you now
public void createDatabase() {
try
{
// To obtain .db The absolute path to the file
String databaseFilename = DATABASE_PATH + DATABASE_FILENAME;
File dir = new File(rootDirectory);
// If the directory does not exist, create it
if (!dir.exists())
dir.mkdir();
// If the /data/data/org.itec.android.Classroom
// Does not exist in the directory .db File from res\raw Copy this file into the directory
if (!(new File(databaseFilename)).exists()){
// For packaging .db Of the file InputStream object
InputStream is = getResources().openRawResource(R.raw.mydb);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[7168];
int count = 0;
// Begin to copy .db file
while ((count = is.read(buffer)) > 0){
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
}
catch (Exception e){
}
}