File Download Implementation
in Mobile Apps
File download is more than just an API call. Distinguish between two fundamentally different scenarios: fast retrieval of small resources (images, documents up to 10 MB) directly in memory, and downloading large files (videos, archives up to 10 GB) with progress indication and resume capability. In 80% of apps, file download functionality is necessary. Mixing these approaches is a common mistake that leads to OOM crashes or a hanging indicator with no feedback. We've implemented this logic in 50+ projects over 10 years on the market—we guarantee stable operation even on low-end devices.
How to Avoid OOM When Downloading Large Files?
The main cause of OOM is attempting to load the entire file into memory. For files >10 MB always use streaming write to disk. On Android—OkHttp with ResponseBody.byteStream() and writing to a file via FileOutputStream in a background coroutine. On iOS—URLSession.downloadTask saves to a temporary file automatically. For Flutter—dio with receiveTimeout option and writing via File. In practice, OOM occurs when downloading files >50 MB without buffering.
Why Progress Bar May Not Work?
The progress bar shows correct percentages only if the server returns the Content-Length header. If missing, the indicator will just spin without numeric value. In such cases you can show an indeterminate progress (activity indicator) or download the file in chunks via Range requests if the server supports resume. Over 30% of servers do not send Content-Length, so test against a real backend.
Mobile File Download by Platform
Compare approaches on three main platforms:
| Platform | Small Files | Large Files | Resume | Background |
|---|---|---|---|---|
| Android | OkHttp/Retrofit | DownloadManager or WorkManager | DownloadManager (partial) | DownloadManager / WorkManager |
| iOS | URLSession.dataTask | URLSession.downloadTask (background) | Manual via URLSession | URLSession background configuration |
| Flutter | Dio | flutter_downloader | Dio (manual) | flutter_downloader |
Android. For in-memory download—OkHttp or Retrofit with ResponseBody.byteStream(), write data to file in an IO coroutine. For large files, system DownloadManager with notification in status bar—user sees progress even after leaving the app. DownloadManager is 2x more efficient than a custom download service for large files. Alternative—WorkManager with custom Worker if more control is needed.
Saving to Downloads folder on Android 10+: use MediaStore API for public files, getExternalFilesDir() for private ones. Attempting to write directly to /sdcard/Download/ without MediaStore on modern versions will throw SecurityException.
Android DownloadManager code example
val request = DownloadManager.Request(Uri.parse(url)) .setTitle(fileName) .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) val downloadId = downloadManager.enqueue(request) iOS. URLSession.downloadTask saves to a temporary file, after which you need to move it to FileManager.default.urls(for: .documentDirectory). For background downloads—URLSessionConfiguration.background(withIdentifier:) with delegate URLSessionDownloadDelegate. Without a background session, download stops when the app goes to background.
let config = URLSessionConfiguration.background(withIdentifier: "com.app.download") let session = URLSession(configuration: config, delegate: self, delegateQueue: nil) let task = session.downloadTask(with: URL(string: url)!) task.resume() Implement urlSession(_:downloadTask:didFinishDownloadingTo:) to move the file and urlSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:) for progress.
Flutter: package dio with onReceiveProgress, saving via path_provider. For background download—flutter_downloader, which wraps native DownloadManager (Android) and URLSession (iOS).
Details Often Overlooked
A file may download partially due to disconnection. Resumable download via Range header (Range: bytes=1048576-) works only if the server returns Accept-Ranges: bytes and Content-Range. If server does not support it, download starts over. Before implementing resume, test backend behavior—this can save up to 30% download time on unstable connections. On average, 15% of download attempts fail on mobile networks, so proper error handling is critical.
Also important to show real progress, not deterministic. If Content-Length header is missing, progress bar will spin without percentages. In that case, better use an indeterminate indicator.
Work Process
- Requirements analysis: what files, max size, need resume and background download.
- Stack selection:
DownloadManagerorURLSessionbackground with configuration. - Design: storage scheme, error handling, progress dialog.
- Implementation: coding with platform specifics (ProGuard/R8, App Transport Security).
- Testing: on real devices with varying network speed and interruptions.
- Deployment: publish to App Store and Google Play with crash log debugging.
Typical Download Errors
- Downloading on main thread—guaranteed ANR on Android.
- Ignoring
Content-Type—file may be saved without extension. - Not cleaning temporary files—
Documents & Datagrows. - Not releasing
URLSessionafter background task on iOS—memory leak.
What's Included in the Work
We select the approach for the task (in-memory vs file, foreground vs background), implement progress, saving to the desired directory, network error handling, and temporary file cleanup. We handle download end-to-end with all platform specifics—from App Store Review Guidelines to ProGuard rules.
Timeline: 1–3 days depending on requirements for resume and background behavior. Our basic implementation starts at $500, with complex projects averaging $850. This saves you up to 40% compared to in-house development. Contact us to evaluate your project—write to us, we'll propose an optimal solution considering your stack.
Documentation: URLSession Programming Guide, DownloadManager Reference







