我有一个sdcard上的数据库文件,现在当下载新的数据库文件时,我需要它覆盖旧的数据库而不是重命名它 这是我的下载代码。下载代码工作正常..
public void startDownload() {
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading Database. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setCancelable(false);
Uri uri=Uri.parse(file_url);
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
File ADirectory = new File(baseDir +"/test");
ADirectory.mkdirs();
lastDownload=
mgr.enqueue(new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(" Database File")
.setDescription("Please wait....test Database downloading.")
.setDestinationInExternalPublicDir("/test", "/testing"));
pDialog.show();
//v.setEnabled(false);
// findViewById(R.id.query).setEnabled(true);
}
答案 0 :(得分:12)
当您将文件提交给下载管理器时,请检查该文件是否已存在。然后删除它。
private boolean isFileExists(String filename){
File folder1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + filename);
return folder1.exists();
}
private boolean deleteFile( String filename){
File folder1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + filename);
return folder1.delete();
}