title: DownloadManager: Freshly downloaded file disappears by itself
body: <p>I've a very odd problem using DownloadManager.</p>
  
  <p>I download some .zip files into /mnt/sdcard/Download.</p>
  
  <p>BroadcastReceiver.onReceive() gets called, and checking if the downloaded file exists returns true.</p>
  
  <p>However, a few moments later the file is gone. </p>
  
  <p>Though I don't delete it. <strong>Permission WRITE_EXTERNAL_STORAGE</strong> is set.</p>
  
  <p>Any ideas what could cause this problem or how to fix it...?</p>
  
  <p>Edit: Here's the code:</p>
  
  <pre><code>public class DownloadUtil {
  private DownloadTask task = null;
  private long downloadQueueId;
  private DownloadManager downloadManager;
  private DownloadCallback callback = null;
  
  
  public DownloadUtil(DownloadCallback callback) {
      this.callback = callback;
  }
  
  public interface DownloadCallback {
      public void onDownloadComplete(String filename);
  }
  
  /** 
   * Call this to download a file.
   */
  public void downloadFile(String address, String destName) {
      if (task != null) {
          return;
      }
  
      task = new DownloadTask(address, destName);
      task.execute((Void) null);
  }
  
  /**
   * Represents an asynchronous login/registration task used to download.
   */
  public class DownloadTask extends AsyncTask&lt;Void, Void, Boolean&gt; {
      String address;
      String destName;
  
      public DownloadTask(String address, String destName) {
          this.address = address;
          this.destName = destName;
      }
      @Override
      protected Boolean doInBackground(Void... params) {
          boolean retVal = true;
  
          try {
              download(address, destName);
          } catch (Exception e1) {
              retVal = false;
              e1.printStackTrace();
              Log.e("DownloadUtil.java", "Exception downloading: ", e1);
          } 
  
          return retVal;
      }
  
      @Override
      protected void onPostExecute(final Boolean success) {
          task = null;
  
          if (success) {
              // finish();
          } else {
  
          }
      }
  
      @Override
      protected void onCancelled() {
          task = null;
      }
  }
  
  
  /**
   * Performs the actual download.
   */
  private boolean download(String address, String destName) throws ClientProtocolException, IOException {
      boolean retVal = false;
  
      BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
          public void onReceive(Context ctxt, Intent intent) {
              String action = intent.getAction();
              if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                  Query query = new Query();
                  query.setFilterById(downloadQueueId);
                  Cursor c = downloadManager.query(query);
                  if (c.moveToFirst()) {
                      int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                      if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                          String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                          Log.i("DownloadUtil.java", "File downloaded to: "+uriString);
  
                          uriString = uriString.substring(7);
                          File f = new File(uriString);
                          Log.i("DownloadUtil.java", "File: "+uriString+" exits: "+f.exists());
                          callback.onDownloadComplete(uriString);
                      }
                  }
                  TabLayoutActivity.context.unregisterReceiver(this);
              }
          }
      };
  
      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(address));
      request.setDescription("Download Request");
      request.setTitle("Download");
      request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, destName);
  
      downloadManager = (DownloadManager) TabLayoutActivity.context.getSystemService(Context.DOWNLOAD_SERVICE);
      TabLayoutActivity.context.registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
      downloadQueueId = downloadManager.enqueue(request);
  
      return retVal;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Edit 2: Can this behavior be caused by the remote server? I've just noticed that the code is working well if I download exactly the same file from one of my servers. It seems to cause problems only if the file is downloaded from the customers server.</p>