安裝時加入外部資料庫樣本(android外部資料庫)_Android

來源:互聯網
上載者:User

android打包安裝時加入外部資料庫,我有這個需求所以就寫了下面的代碼,現在分享給大家

複製代碼 代碼如下:

public void createDatabase() {
  try 
  {  
   // 獲得.db檔案的絕對路徑  
   String databaseFilename = DATABASE_PATH + DATABASE_FILENAME;  
   File dir = new File(rootDirectory);  
   // 如果目錄不存在,建立這個目錄  
   if (!dir.exists())  
    dir.mkdir();  
   // 如果在/data/data/org.itec.android.Classroom
   //目錄中不存在 .db檔案,則從res\raw目錄中複製這個檔案到該目錄  
   if (!(new File(databaseFilename)).exists()){  
    // 獲得封裝.db檔案的InputStream對象  
    InputStream is = getResources().openRawResource(R.raw.mydb);  
    FileOutputStream fos = new FileOutputStream(databaseFilename);  
    byte[] buffer = new byte[7168];  
    int count = 0;  
    // 開始複製.db檔案  
    while ((count = is.read(buffer)) > 0){  
     fos.write(buffer, 0, count);  
    }  
    fos.close();  
    is.close();  
   }
  }  
  catch (Exception e){  
  }
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.