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...)
Related
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
I am generating an excel sheet from search results in my app when user selects 'generate excel' option provided in my app overflow actionbar. I could save that file in the location "file:///storage/emulated/0/Download/tempfile.xls" without any problem but I would like to provide the generated excel sheet in the download menu via DownloadManager like this -
String filepath = "file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/tempfile.xls";
Uri uriDownloadFile = Uri.parse(filepath );
DownloadManager.Request request = new DownloadManager.Request(uriDownloadFile);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "tempfile.xls");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // to notify when download is complete
request.allowScanningByMediaScanner();// if you want to be available from media players
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
manager.enqueue(request);
I tried to change the the local filepath to a uri using Uri.parse (second line of code above). However, I ended with the following error -
java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: file:///storage/emulated/0/Download/tempfile.xls
Based on another SO answer, I am using the following lines of code to download media files:
DownloadManager.Request r = new DownloadManager.Request(Uri.parse(uri));
// This put the download in the same Download dir the browser uses
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_PODCASTS, fileName);
// When downloading music and videos they will be listed in the player
// (Seems to be available since Honeycomb only)
r.allowScanningByMediaScanner();
// Notify user when download is completed
// (Seems to be available since Honeycomb only)
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// Start download
DownloadManager dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(r);
Now, that is all good except that I have a few questions:
The person who answered said that the notifications can be customized. How can that be done?
If I want to add a "cancel" button to cancel the download and delete the file, how do I do it? What do I need to implement to get this behavior? :)
// Changing the Notification Title, Description, and its Visibility
DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("///.mp3");
DownloadManager.Request req = DownloadManager.Request(uri);
req.setTitle("Download");
req.setDescription("Kick Ass Description");
req.setVisibility(Request.VISIBILITY_VISIBLE_COMPLETION_ONLY);
Pretty self-explanatory.
I'm trying to set an icon for the DownloadManager request that I created, but I can't manage to do that.
I read about creating a BroadcastReceiver, but I was wondering if it's possible to do that without it.
This is my code for the DownloadManager Request:
final String filename = getIntent().getExtras().getString("id") + ".apk";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(filename);
request.setDescription("Downloading...");
request.setMimeType("application/vnd.android.package-archive");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir("/Library", filename);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Toast.makeText(LinkActivity.this, "Download has started.", Toast.LENGTH_LONG).show();
Here's an example of what it looks like now:
My target is to change the image of the notification ImageView on the left.
Thank you very much.
You cannot override this icon. And, if I remember correctly, it would make no much sense to be able to, as download manager can have more than one downloading running and it will still take one notification slot to indicate that...
I think i have a rather simple question.
http://www.vogella.com/blog/2011/06/14/android-downloadmanager-example/
I have been following a tutorial in the above url.
How do I change the filepath for downloads?
Thanks in Advance
You configure the DownloadManager.Request object with that sort of information. In the tutorial, that Request object is created and used in onClick().
For example:
DownloadManager.Request req=new DownloadManager.Request(uri);
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
"test.mp4");
(above code is from this sample project)
The last line in CommonsWare's answer states the destination. He just uses the regular download-folder on the sdcard, but you could as well do this:
req.setDestinationInExternalPublicDir("/mnt/sdcard/Myfolder", "file_name.extension");
You make sure external storage access is permitted by user for your app. Include this code for the download
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url_string);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setVisibleInDownloadsUi(true); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// include this line if permission has been granted by user to external directory
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
// or this line if not yet granted
request.setDestinationUri(Uri.parse("file://" + uri.getLastPathSegment());
Long reference = downloadManager.enqueue(request);
You can try to do following:
Visit the URL to understand about Download Manager https://github.com/quoraboy/DownloadManagerInAndroid
As far as I understand you can't pause/resume download manually.
If you cancel the download then it totally depends on your server whether they support pause/resume feature or not. If yes then, after cancelling the download, if you start the download again it may actually resume it and don't start downloading from beginning.