如何將SQLite資料庫(dictionary.db檔)與apk檔一起發佈?

答: 把這個檔放在/res/raw目錄下即可。res\raw目錄中的檔不會被壓縮,這樣可以直接提取該目錄中的檔,會生成資源id。

那麼如何把raw檔下面的資料庫導入到安裝的程式中的database目錄下呢?

public void imporDatabase() {
//存放資料庫的目錄
String dirPath="/data/data/com.hkx.wan/databases";
File dir = new File(dirPath);
if(!dir.exists()) {
dir.mkdir();
}
//資料庫檔案
File file = new File(dir, "abc.db");
try {
if(!file.exists()) {
file.createNewFile();
}
//載入需要導入的資料庫
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();
}
}

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()