在我的应用程序中,我通过下载管理器类从服务器下载数据库,并将其默认存储在' sdcard'中,因此我想将此下载的数据复制到“sdcard'到数据/数据文件夹。那么,任何人都可以帮助我吗?
答案 0 :(得分:1)
尝试以下功能将数据库从SD卡复制到您的应用程序包。
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "<your sdcard path>";
String backupDBPath = "data/data/"+ "<yourpackagename>" +"/databases/"+SAMPLE_DB_NAME;;
File currentDB = new File(sd, currentDBPath);
File backupDB = new File(data, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch(IOException e) {
e.printStackTrace();
}
}
根据您的数据库下载路径以及要备份它的路径更改currentDBPath
和backupDBPath
。