我正在創建一個使用現成的SQLiteDatabase
的應用程序。我正在給用戶選擇將數據庫複製到SD卡或手機內存中。將數據庫複製到手機內存時,以下代碼可以很好地工作。 26.5 MB數據庫完全複製到手機內存中。但是,如果使用相同的代碼複製SD卡,則只複製數據庫的一部分,即只複製名爲android_metadata
的一個表。複製到SD卡時數據庫部分複製
private void copyDatabase(String storage) {
try {
//DB_NAME is defined in the class
//DB_PATH refers to the phone directory path.
InputStream in = myContext.getAssets().open(DB_NAME);
File sd = Environment.getExternalStorageDirectory();
File sdCardPath = new File(sd, DB_NAME);
File phonePath = new File(DB_PATH + DB_NAME);
OutputStream os = null;
if (storage.equalsIgnoreCase("sd"))
os = new FileOutputStream(sdCardPath);
else if (storage.equalsIgnoreCase("phone"))
os = new FileOutputStream(phonePath);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
問題是,雖然數據庫是在SD卡內存中創建的,但是不會複製完整的數據庫。這樣創建的數據庫只有3.4 Mb的大小,而原始數據庫的大小是26.5 Mb,我通過Eclipse中的文件資源管理器視圖檢查了這個數據庫的大小。
有什麼建議嗎?
編輯: 由於我直接從assets
文件夾複製數據庫,我才能獲取FileInputStream
或FileChannel
對象。我只能從assets
資源中獲得一個InputStream
對象。
我搜索了谷歌並偶然發現了您提供的鏈接。但兩個問題的區別在於我直接從'assets'文件夾複製數據庫,因此無法獲得'FileInputStream'。另一方面,另一個問題從手機存儲器複製數據。 – Rajat
哦,好吧,我會試着回答一些問題,並且會回來 –