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 :
- Some files are re-downloaded and and saved with '-1' suffix
- 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!
- The events on 'onReceive' is pretty whacky too which takes some tries and hack to get it right
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
1 if(status == DownloadManager.STATUS_SUCCESSFUL)
2 {
3 File fileTmp = new File(fileNameTmp);
4 File file = new File(fileName);
5
6 if(file.exists())file.delete();
7 fileTmp.renameTo(file);
8 }
4 comments:
Hi,
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.
Cheers,
Andreas
Hi Andreas,
It was deleted from the disc. The file has been succesfully downloaded to the disc previously but then got deleted.
Hi,
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.
Cheers,
Andreas
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.
Cheers
B
Post a Comment