Android DownloadManager not deleting the physical file when remove is called - android

I am noticing some strange behaviour while using DownloadManager class to download videos from my server.
The videos are downloaded just fine, but when I try to delete them using the public int remove (long... ids) function, the physical file doesn't get deleted and memory ultimately isn't released either.
Here's how i am giving the DownloadManager.Request object to the DownloadManager's enqueue function:
Uri target = Uri.fromFile(new File(destinationFolder, Sha1Util.SHA1(url)));
Request request = new Request(Uri.parse(url));
request.setDestinationUri(target);
request.setNotificationVisibility(Request.VISIBILITY_HIDDEN);
request.setVisibleInDownloadsUi(false);
where destinationFolder = /storage/emulated/0/Android/data/{app's_package_name}/{user_name}
NOTE: The remove function is working fine on Lollipop devices but isn't working on prior versions.

Related

Android Download Manager giving 400 'placeholder' error in androidX kotlin

I am trying to download some file using android download manager, it always gives 400 error, and download unsuccessful message.
fun DownloadData(uri: Uri): Long {
val downloadReference: Long
downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val request = DownloadManager.Request(uri)
//Setting title of request
request.setTitle("Test Download")
//Setting description of request
request.setDescription("Android Data download using DownloadManager.")
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(this#MainActivity, Environment.DIRECTORY_DOWNLOADS, "AndroidTutorialPoint.mp3")
//Enqueue download and save the referenceId
downloadReference = downloadManager!!.enqueue(request)
return downloadReference
}
I am calling above function by
val uri = Uri.parse("https://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_1920_18MG.mp4")
downloadId = DownloadData(uri)
unable to understand where am i going wrong? the same code works in API level 26, it stopped working after migrating to androidx, and api 29
EDIT: it doesnt work on API level 28 and later irrespective of androidX, shows download unsuccessful

DownloadManager enqueue queue file and keeps on downloading in Oreo and above

I am using DownloadManager to download pdf file in my application. In devices with OS version of Oreo and above, the file is queued for download and keeps on downloading. But after some time nothing happens and no error or exception is shown. The same works fine in the device of OS version below Oreo.
Below is the code used.
private void startDownload(String downloadPath, String destinationPath) {
Uri uri = Uri.parse(downloadPath); // Path where you want to download file.
File destinationFile = new File(Environment.getExternalStorageDirectory() + "/folder_name");
destinationFile.mkdir();
destinationFile = new File(destinationFile.getAbsolutePath(), "file_name.pdf");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); // Tell on which network you want to download file.
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // This will show notification on top when downloading the file.
request.setTitle("Downloading a file"); // Title for notification.
request.setVisibleInDownloadsUi(true);
request.setDestinationUri(destinationFile); // Storage directory path
((DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); // This will start downloading
}
Have enabled required permissions and tried setting Network type as well but no luck. Please help me with this.
Thanks in advance.

Android doesn't grab new json file

I am developing an app that grabs a new json file every time is starts. But I've found that even though I delete the file and download the new one, the phone somehow still ends up with the old file.
Delete code:
if (test.exists()) {
test.delete();
Log.i(TAG, "Deleting File");
} else {
Log.i(TAG, "File does not exist");
}
Download Code:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(path));
request.setTitle("stations.json");
request.setDescription("File is being downloaded.....");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setDestinationInExternalFilesDir(getApplicationContext(), null, "stations.json");
request.setVisibleInDownloadsUi(false);
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
jsonRef = manager.enqueue(request);
I've deleted the file myself from the storage. It still grabs the old file. I've checked in a browser if the server version is updated. It was. I'm at a loss as to what may be happening. If the Stack community has any suggestions, it would be greatly appreciated. Thanks so much!
It appears that my phone was somehow caching the file. It would see that I was downloading the same file and would instead just give it to me. By giving it a new url, it tricked the app into thinking it was a new file.
In the end, what worked was to add:
?v=" + Math.random()
to the end of the file.
For Example:
String path = example.com/stations.json?v=" + Math.random();
Then download with:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(path));

Android Download Manager won't stop

I'm not exactly sure what my problem is. I using the code below to download a bunch of files:
public void DownloadNow(ArrayList<String> must_download)
{
String serviceString = Context.DOWNLOAD_SERVICE;
DownloadManager download;
download = (DownloadManager)getSystemService(serviceString);
for(int x = 0; x < must_download.size(); x++)
{
System.out.println("DOWNLOADING "+must_download.get(x).toString());
Uri uri = Uri.parse("http://drm.csdev.nmmu.ac.za/Zandre/"+must_download.get(x).toString());
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, must_download.get(x).toString());
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
request.setTitle(must_download.get(x).toString());
long reference = download.enqueue(request);
}
}
Where must_download is an ArrayList full of file names. This method has successfully worked in the past, until I tried to run it on a different thread. The thread idea did not work out so well, so I decided to switch back.
Now, the problem is this- since I switched back to the original method the Download Manager runs full time, downloading the same files over and over in an endless loop.The only way I can stop it is to **Disable the Download Manager, but then I can't download anything...
What I've read so far and tried is
Uninstall the app- still downloading...
Switch device off and back on again- still downloading...
Clear cache of Download Manager- still downloading...
Even after this has used all the devices mobile data, it still just carries on downloading.
Has anyone else encountered this type of problem? Any ideas solutions?

Download file to external public storage without knowing the filename

My HTTP-server allows downloading files with a 'dynamic' url: e.g. http://myserver.com/query?id=12345 which will give me my_song.mp3.
The filename is indicated in the content-disposition header.
Downloading this kind of file with Android DownloadManager works fine but I want to be able to control where the file is being saved to.
The normal way to do this would be to call
DownloadManager.Request r = new DownloadManager.Request(uri);
r.setDestinationInExternalPublicDir(String dirType, String subPath);
Unfortunately this requires to know the filename up front which I don't know. I tried calling the above function with a null for subPath but it does not work...

Categories

Resources