16

我在 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);
  } 
4

1 回答 1

20

当您将文件提交给下载管理器时,请检查该文件是否已存在。然后删除它。

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();


}
于 2013-08-15T00:57:08.843 回答