Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i make download manager overwrite an exisiting file instead of renaming the file in android

Tags:

android

I have a database file which is on the sdcard and now when a new database file is downloaded i need it to overwrite the old one instead of renaming it here is my downloading code. The download code is working perfectly..

  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);
  } 
like image 919
Simon Macha Mwas Avatar asked Jan 16 '13 16:01

Simon Macha Mwas


1 Answers

when you're submitting the file to the download manager check if the file already exists. then delete it.

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


}
like image 195
user2668894 Avatar answered Nov 04 '22 15:11

user2668894