須藤代碼複製次e數據庫到SD卡。要將其複製回來,只需顛倒流。
public boolean copyDatabase() {
String SDCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
// Create the directory if neccesary.
File directory = new File(SDCardPath + <PATH TO SD-CARD SAVE LOCATION>);
if (!directory.exists())
directory.mkdir();
// Close the database before trying to copy it
database.close();
// Copy database to SD-card
try {
InputStream mInput = new FileInputStream(<PATH TO DATABASE ON PHONE>);
OutputStream mOutput = new FileOutputStream(SDCardPath + <PATH TO SD-CARD SAVE LOCATION>);
byte[] buffer = new byte[1024];
int length;
while ((length = mInput.read(buffer)) > 0) {
mOutput.write(buffer, 0, length);
}
mOutput.flush();
mOutput.close();
mInput.close();
} catch (Exception e) {
}
return database.open();
}