Downloaded files get deleted automatically

It was too big for a comment to I'm just putting it as an answer.

I don't have any clue yet why the files are missing or getting deleted over a period of time - which a bit strange. But I had to do something similar and never experienced something like this. So I though I might share what I have done in that case.

I preferred to keep the downloaded files in the internal storage which the application holds rather than keeping it in the external storage. For example, the file path where I used to save the downloaded files is something like...

/data/data/" + "com.example.myapplication" + "/musicfiles/

So what I did right after downloading is telling the media scanner about the new file so that it is immediately available to the user. So as I was downloading the file using an AsyncTask I had to run the scanner in the onPostExecute method like this after a successful download.

protected void onPostExecute() {

    if(downloadCompletedSuccessfully) {

        MediaScannerConnection.scanFile(context,
            new String[]{file.toString()}, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Log.i("ExternalStorage", "Scanned " + path + ":");
                    Log.i("ExternalStorage", "-> uri=" + uri);
                }
            });
    }
}

So in your case, I think its not might be the issue that the Download button is not visible due to the unavailability of the file downloaded. It might happen if the media scanner fails to find the file in the file system as well. So you might consider giving it a try.

I don't know if clean master or some softwares like that cleans junk files every now and then if they are in external storage. You might consider investigate those as well.


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


There is a bigger problem starting in Android Q where value set by setVisibleInDownloadsUi is ignored and it is false by default. The files will be deleted after a week. You can test it by: download file, change date in calendar by 2 months for example, call this in terminal:

adb shell cmd jobscheduler run -f com.android.providers.downloads -100

It starts job from DownloadIdleService where old and forbidden files are deleted. You can see in logcat (set no filter) that DownloadManager try to delete your files.

To prevent from that you can:

  • download files to Environment#getExternalStoragePublicDirectory(String) with Environment#DIRECTORY_DOWNLOADS) - this is the only path starting in Android Q where downloads are visible and should not be deleted
  • change file name after downloading (in DownloadedReceiver for example)