Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloaded files get deleted automatically

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);
like image 242
Sagar Avatar asked Sep 30 '17 10:09

Sagar


People also ask

Why are my downloaded files automatically deleted?

When you enable it, Windows will automatically delete unused files when the computer is low on disk space. For instance, it can automatically delete files older than 30 or 60 days from the Recycle Bin or delete temporary files to free up some space. It also can delete older files in the Downloads folder.

Why are my files deleting?

To solve the "my computer is automatically deleting files in Windows 10" problem, you can turn off the Storage Sense feature in Windows 10. This feature will automatically delete unused files and temporary files as well as old files in the Downloads folder and Recycle Bin regularly.

Do files in downloads get deleted?

If you delete your Downloads folder, you will lose the files and folders inside it. However, there is a chance that it has simply been moved to the Recycle Bin. Try to find your files in there, and if you do, you can safely restore them.


2 Answers

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)
like image 114
Mateusz Kaflowski Avatar answered Oct 19 '22 06:10

Mateusz Kaflowski


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

like image 22
user3209486 Avatar answered Oct 19 '22 05:10

user3209486