I am using the DownloadManager to download some large files from a server in my app, and I need to be able to prompt the user to download a file again if the download does not complete. I have an object called FileData which contains information about the file, such as the URL where it should be downloaded, and the size of the file.
The approach I have in place right now is that when I restart the app I call:
final File fsFile = new File(getDataFilePath(file));
if (fsFile.exists() && fsFile.length() != file.size) { // file is a variable that refers to a on object that has file information
fsFile.delete();
}
However, it appears that this approach fails because the entire file is allocated when the initial download begins, and so file size is not an accurate way to determine whether a file download completed.
Other approaches I have thought of:
Query the DownloadManager for downloads with STATUS_FAILED. This has the additional overhead of checking if there is a subsequent STATUS_SUCCESSFUL for a given file (since I imagine the DownloadManager retains the history of downloads for quite a while), among other things, so it doesn't seem like the most straightforward option.
Use an MD5 checksum to check whether the complete file has been downloaded. This has the disadvantage of having to make a network request to determine whether the file download failed.
At the moment, all my file downloads are sequential, so I could just store the name of the file that was downloading and the request ID for that download in the user preferences, and then check if that download completed when the app restarts. This seems like the cleanest and easiest approach.
Barring any other ideas I will go with option 3, but I was wondering if maybe there was a simple approach to this that I was missing. Any suggestions would be helpful!
0 comments:
Post a Comment