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() {
   //  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())   
   //  If the /data/data/
   // 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 = > 0){   
     fos.write(buffer, 0, count);   
  catch (Exception e){   

Related articles: