Downloaded files get deleted automatically

Sagar picture Sagar · Sep 30, 2017 · Viewed 12.8k times · Source

In my app, there are a few files that users can download. The files get downloaded via the android download manager. But, since a few weeks now, hundreds of users have been complaining that their files automatically keep deleting every 8-12 days, without them even uninstalling the app. (There might be many more users who haven't bothered to complain about the same.)

Now, there could be a number of user-specific reasons why that would happen on a few devices. But considering the huge number of users, it seems that I might have been doing something wrong.

Why would the system/download manager delete the files automatically? Is there a way to inform the system or the download manager to not delete certain files? Or should I just settle with renaming the files after downloading, so as to unlink them from the download manager, and hope that the problem gets solved with just that?

Edit:

Here's the code that I use to download the files:

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(trackLink));
request.setTitle(trackTitle);
request.setDestinationInExternalPublicDir("Tracks", trackTitle + ".mp3");
request.setVisibleInDownloadsUi(false);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

Answer

user3209486 picture user3209486 · Jan 6, 2018

I also came across this issue. Looking at the source for DownloadIdleService it appears that if the download are set to not be visible in the UI they're deleted after 7 days as they're considered "stale". Here's the javadoc fromDownloadIdleService:

/**
 * Remove stale downloads that third-party apps probably forgot about. We
 * only consider non-visible downloads that haven't been touched in over a
 * week.
 */

https://android.googlesource.com/platform/packages/providers/DownloadProvider/+/master/src/com/android/providers/downloads/DownloadIdleService.java#110