问题
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);
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/46501822/downloaded-files-get-deleted-automatically