Android DownloadManager: Download fails, but COLUMN_REASON only returns "placeholder"

hez picture hez · Sep 19, 2012 · Viewed 10.3k times · Source

DownloadManager appears to be the right choice for an app with lots of background downloads on a flaky mobile internet connection.

Using tutorial code found on the web, the app is able to request a download from the system's DM like so:

// in onCreate()
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

// in requestData()
Uri u = Uri.parse("http://server:8000/feed/data");
Request dreq = new Request(u);
dreq.setNotificationVisibility(Request.VISIBILITY_HIDDEN);
downloadID = dm.enqueue(dreq);

The URL in that code is a test server on a local computer. The URL works, the browser in Android emulator can retrieve the page and the server returns HTTP code 200 when my app requests that page via DownloadManager and the code quoted above.

This is the relevant code in the ACTION_DOWNLOAD_COMPLETE BroadcastReceiver which gets called when DM has retrieved the file.

Query q = new Query();
q.setFilterById(downloadID);
Log.i("handleData()", "Handling data");
Cursor c = dm.query(q);
if (c.moveToFirst()) {
  Log.i("handleData()", "Download ID: " + downloadID + " / " + c.getInt(c.getColumnIndex(DownloadManager.COLUMN_ID)));
  Log.i("handleData()", "Download Status: " + c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)));
  if (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
    String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
    Log.i("handleData()", "Download URI: " + uriString);
  } else if (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) {
    Log.i("handleData()", "Reason: " + c.getString(c.getColumnIndex(DownloadManager.COLUMN_REASON)));
  }
}

The strange result is this:

The DOWNLOAD_STATUS is 16 (or STATUS_FAILED), but the reason is "placeholder".

Why is that? Why does it fail when the server returned a 200 status code? And why is there no reason given by DownloadManager?

Answer

hez picture hez · Sep 19, 2012

Answering myself here.

Here's the problem: COLUMN_REASON is not a string, but a number value.

Log.i("handleData()", "Reason: " + c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON)));

will return an actual error code that one can work with.