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

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

Related

Download Manager notification not showing when downloading a file

I have an application where i have a feature to download a file already stored in firebase. i have implemented the code to download using download manager and at the same time show the user the notification of downloading that file but for some reason it is not working.
This is my code
val downloadRequest = DownloadManager.Request(Uri.parse(model.document.substringBeforeLast(".")))
downloadRequest.setTitle("الملف")
downloadRequest.setDescription("تحميل الملف")
downloadRequest.setVisibleInDownloadsUi(true)
downloadRequest.setDestinationUri(uri.toUri())
downloadRequest.allowScanningByMediaScanner()
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadManager.enqueue(downloadRequest)
Is the file getting downloaded but notification is not visible or file is not getting downloaded either?
try this and let me know if it still doesn't work :)
downloadRequest.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE
)
EDIT
it seems that the last time I used it there was a difference in the destination that you are specifying.
val directory: File = File(Environment.getExternalStorageDirectory().path + "/myFile")
if(!directory.exists()) {
directory.mkdir()
}
...
downloadRequest.setDestinationInExternalPublicDir("/myFile","android")

Prevent deleting files downloaded with DownloadManager on app uninstall

I've noticed that files downloaded using DownloadManager get deleted if my app gets uninstalled.
Is there a way to prevent this?
Update:
Here is kotlin code that I use to enqueue download:
val downloadId = DownloadManager.Request(Uri.parse(downloadUrl)).run {
setTitle(title)
setDescription(title)
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
setAllowedOverMetered(true)
setAllowedOverRoaming(true)
setDestinationUri(file)
downloadManager.enqueue(this)
}
... where file is of the form: /storage/<sdcard-id>/Movies/<filename>.mp4

android DownloadManager notification not visible

i'm trying to download a file using the DownloadManager, the download starts but no notification is viewed. tried only on android API 27
val url="http://exmaple.com/mp4.mp4" //a fabricated url
val request = DownloadManager.Request(Uri.parse(url))
.setTitle("Dummy File")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
.setAllowedOverMetered(true)
.setVisibleInDownloadsUi(true)
val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
downloadManager.enqueue(request)
The problem is not with your code here I found this when I try.
It because of sample URL that you tried to download is not permitted one.
W/DownloadManager: [151] Stop requested with status BAD_REQUEST: Cleartext traffic not permitted for UID 10202: http://exmaple.com/...
Try "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4"
sample video it is working fine.
Happy coding...)

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 DownloadManager not deleting the physical file when remove is called

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.

Categories

Resources