1
我找到了一個tutorial,它將數據庫作爲備份複製到文件夾,並將其作爲還原複製迴應用程序的數據庫文件夾。幾乎一切正常,除了一件事情:當我從我的文件夾中刪除已備份的.db文件,然後單擊應用程序中的還原時,它將查找該文件,找不到它,然後它將不會覆蓋整個數據庫。這就像我清空了整個數據庫。Android - 恢復數據庫在數據庫文件丟失時清除數據庫
備份:
try {
InputStream myInput;
OutputStream myOutput;
myInput = new FileInputStream("/data/data/com.me.myapp/databases/HotOrNotdbLists2");//this is
// the path for all apps
//insert your package instead packagename,ex:com.mybusiness.myapp
// Set the output folder on the SDcard
File directory = new File("/sdcard/MyApp/");
// Create the folder if it doesn't exist:
if (!directory.exists())
{
directory.mkdirs();
}
// Set the output file stream up:
myOutput = new FileOutputStream(directory.getPath()+
"/myapp.db");
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
// Close and clear the streams
myOutput.flush();
myOutput.close();
myInput.close();
Toast.makeText(Settings.this, "Backup done successfully!", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Toast.makeText(Settings.this, "Backup unsuccessful! File cannot be created! Directory does not exist?", Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(Settings.this, "Backup unsuccessful!", Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
e.printStackTrace();
}
還原:
OutputStream myOutput;
try {
myOutput = new FileOutputStream("/data/data/com.me.myapp/databases/HotOrNotdbLists2");
// Set the folder on the SDcard
File directory = new File("/sdcard/MyApp/");
// Set the input file stream up:
InputStream myInputs = new FileInputStream(directory.getPath()+ "/myapp.db");
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInputs.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
// Close and clear the streams
myOutput.flush();
myOutput.close();
myInputs.close();
Toast.makeText(Settings.this, "Restore done successfully!", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Toast.makeText(Settings.this, "Restore unsuccessful! File not found! Directory does not exist?", Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) { Toast.makeText(Settings.this, "Restore unsuccessful!",
Toast.LENGTH_SHORT).show();
// TODO Auto-generated catch block
e.printStackTrace();
}
編輯: 我加了賈森的回答,它仍然會清除該數據庫,即使我得到假到directoryB並獲得恢復不成功!文件未找到!目錄/文件不存在?消息???!?!
try {
OutputStream myOutput;
myOutput = new FileOutputStream("/data/data/com.me.myapp/databases/HotOrNotdbLists2");
// Set the folder on the SDcard
File directory = new File("/sdcard/MyApp/");
boolean directoryB = new File("/sdcard/MyApp/", "/myapp.db").exists();
Toast.makeText(Settings.this, "directoryB: " + directoryB, Toast.LENGTH_SHORT).show();
if (directoryB == true)
{
Toast.makeText(Settings.this, "File exists!", Toast.LENGTH_SHORT).show();
// Set the input file stream up:
InputStream myInputs = new FileInputStream(directory.getPath()+ "/myapp.db");
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInputs.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
// Close and clear the streams
myOutput.flush();
myOutput.close();
myInputs.close();
Toast.makeText(Settings.this, "Restore done successfully!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(Settings.this, "Restore unsuccessful! File not found! Directory/file does not exist?", Toast.LENGTH_SHORT).show();
}
} catch (FileNotFoundException e) {
//Toast.makeText(Settings.this, "Restore unsuccessful! File not found! Directory does not exist?", Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) { Toast.makeText(Settings.this, "Restore unsuccessful!",
Toast.LENGTH_SHORT).show();
// TODO Auto-generated catch block
e.printStackTrace();
}
我試過了,它仍然覆蓋了應用程序。我認爲情況沒問題,有什麼想法?我更新了主要帖子 – erdomester
將您的支票移到方法的頂部,所以如果文件不存在,您不會執行任何操作。拋出一些日誌以查看文件是否存在(可能是文件未被實際刪除)。 –
多麼想念我..哦!謝謝! – erdomester