Search code examples
androidandroid-download-manager

DownloadManager downloading files over 2.1 GB


I am working on an app and one of the features I am working on is to download some binary files. Some of them are really big (more than several mega-bytes). Downloads are completing fine as long as the file size is less than 2 GB.

I got stuck on a file that is 3.2GB in that I get progress updates (I am pooling the DownloadManager for progress updates), but when the download completes, the file is not present on the target file path. Interrogating the DownloadManager for that download id, I get STATUS_FAILED and reason ERROR_UNKNOWN - the favorite error details one will ever wish for!

What is weird is that this appears on most of the devices, but for some (like Samsung SG 4 Active OS 4.2.2 and LG Nexus 5 OS 4.4.2), it doesn't appear.

Doing some extra investigation, I found out that this seems to be a bug in Android DownloadManager implementation. It seems Android implementation stores the download count as an int, but when that count goes above Integer.MAX_VALUE the download ends as failed.

I am thinking to replace the DownloadManager usage with a foreground service, but I wouldn't give up yet ....

Did you guys face this and if so, how did you fix it? Is there any work-around to use DownloadManager in pre-4.2.2 so I can download more than 2.1 GB per file?


Solution

  • To download such a large files, you need to download those in chunks. Either you can use any library that support HTTP range options to allow to pull down a single file in multiple pieces , supporting resume etc.

    Or you can split your large file on your server then have a text file with MD5 hash of each file, when you first start to download then get the MD5 file once finish then check that hashes matches the downloaded pieces. If they do not then delete that piece and add it to queue of items to download.

    Once all pieces downloaded and MD5 works, you can put the pieces back together as single file.

    If you are thinking to download the file in the SD card then FAT32 is the default file system. There is a 4 GB per file limit with this file system.