Friday, May 24, 2013

Workaround bug in Android's DownloadManager

Bug in Android's DownloadManager service is quite well known. You can check it out here and here for some example. Here is what I found on my use of it :

  1. Some files are re-downloaded and and saved with '-1' suffix
  2. Some other files are re-downloaded but failed on the second download. However, it just not fail but together with it, the first succesfully downloaded files are deleted!
  3. The events on 'onReceive' is pretty whacky too which takes some tries and hack to get it right
Suffice to say it was no fun and I saw hours of work just goes out of window because of it which can be used for more productive work if the API just do what it supposed to do as stated on the documentation. Strangely,  so far there was no good workaround that I can use on my case. Some are overly complicated that I don't see them as justified in term of long-term maintenance cost.

In my case, the first case above (successful double download) is not my primary concern. The duplicates are negligible. However, the second one done some catastrophic damage on how my code are run. So, after trying things here and there, here's what I come up with in pseudocode :
  • prepare two files : the true destination file and temporary destination file
  • download to temporary
  • right after download successful,  rename temporary to original destination file
  • now, in the case Android going into the second case above, it will try to delete temporary file instead and the original destination file is save
Below are the code snippet to be put in onReceive of BroadcastReceiver :

   1 if(status == DownloadManager.STATUS_SUCCESSFUL)
   2 {
   3     File fileTmp = new File(fileNameTmp);
   4     File file = new File(fileName);
   6     if(file.exists())file.delete();
   7     fileTmp.renameTo(file);
   8 }


drisse said...


I think that I have a similar problem that you describe in nr 2 above but I have a supplementary question:
By "deleted" do you mean from the DownloadManager or the disc? Because in my case the I have 3 downloaded files on disc but DowloadManager only contains 1.


Hafiz said...

Hi Andreas,

It was deleted from the disc. The file has been succesfully downloaded to the disc previously but then got deleted.

drisse said...

Thanks a lot for your reply.
Ok, that is not what happens in my case. In my case the files still exists on disc but they are for some reason removed from the DownloadManager and I can't figure out why.
I have successfully downloaded several files and after some time (couple of weeks) they don't exists in the DM anymore.

I have to keep on digging.


Anonymous said...

Hi Drisse, did you find solution to the issue you mentioned? I am getting similar issue, when I kill the app after download manager successfully downloaded - the completed files are getting deleted from disc.

Thanks for ur inputs.