我一直在使用下面的代碼,但直觀地看到它不是太安全。它會在每個設備上完美地工作嗎?此代碼在第一次應用程序運行時將我的主數據庫(100 kB)複製到內部存儲器中。謝謝你的時間。這是將文件保存到內存的安全正確方法嗎?
private void CopyDBtoInternal() {
String path = "/data/data/com.myproject/databases/";
String DB_DESTINATION = "/data/data/com.myproject/databases/sample.db";
// Check if the database exists before copying
boolean initialiseDatabase = (new File(DB_DESTINATION)).exists();
if (initialiseDatabase == false) {
//create folders
File db_folder = new File(path);
db_folder.mkdirs();
AssetManager assetManager = getApplicationContext().getAssets();
try {
InputStream is = assetManager.open("sample.db");
// Copy the database into the destination
OutputStream os = new FileOutputStream(DB_DESTINATION);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0){
os.write(buffer, 0, length);
}
os.flush();
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
AlertDialog.Builder dialogo = new AlertDialog.Builder(AppGuiaDeBulas.this);
dialogo.setTitle("Alerta");
dialogo.setMessage("It was not possible to save database. Error #001");
dialogo.setNeutralButton("OK", null);
dialogo.show();
}
}
}
對不起,忘了提,我使用API等級3爲我的應用程序一個更廣泛的用戶。我想將我的小型數據庫保存到內部存儲器而不是外部存儲器。或者這不是一個好習慣嗎? – Daniel