user2779473
user2779473

Reputation: 131

DownloadManager: Freshly downloaded file disappears by itself

I've a very odd problem using DownloadManager.

I download some .zip files into /mnt/sdcard/Download.

BroadcastReceiver.onReceive() gets called, and checking if the downloaded file exists returns true.

However, a few moments later the file is gone.

Though I don't delete it. Permission WRITE_EXTERNAL_STORAGE is set.

Any ideas what could cause this problem or how to fix it...?

Edit: Here's the 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<Void, Void, Boolean> {
    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;
}

}

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.

Upvotes: 2

Views: 2812

Answers (2)

plin
plin

Reputation: 43

Yes, this is related to the issues listed by @Artjom. There is a bug in DownloadManager in which it will sometimes (often times?) fire off another download request for an already completed download. The ACTION_DOWNLOAD_COMPLETE is also broadcast, but because the subsequent download fails, it removes the download entry and additionally removes the local file.

I've worked around this issue by checking for the existence (+size) and moving the file to another location.

Upvotes: 1

Related Questions