I used DownloadManager in my application to download file from internet.
Here's my code.
DownloadManager downloadManager = (DownloadManager) ui.activity.getSystemService(Activity.DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse("http://dl.appvn.com/appvn.apk");
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
.
request.setAllowedOverRoaming(false);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"appvn.apk");
request.setTitle("AppStoreVN");
request.setDescription("Android Data download using DownloadManager.");
downloadManager.enqueue(request);
I used setTitle & setDescription to change the info show on Notification bar. But it also change my file name like title ("AppStoreVN" while it should be "appvn.apk").
Anyone have ideas? Thanks
for getting name of downloading file i used:
String nameOfFile = URLUtil.guessFileName(mDownloadFileUrl, null,
MimeTypeMap.getFileExtensionFromUrl(mDownloadFileUrl));
then for setting title and description of DownloadManager
request.setDescription(nameOfFile);
request.setTitle(nameOfFile);
Then for setting actual file name of downloaded file in directory path
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
/*here the name of file is set*/nameOfFile);
hope this help you :)
I came here finding what I thought was the same issue as when I look in the "downloads" app on the phone I see the title of the file instead of the .apk
I found if I use a file explorer though the actual filename is correct, it's just the downloads app shows the content database details and must set a title to be the same as the notification title.
If it won't parse the file, look at logcat as the location the file had been created at didn't match what I had expected it to be.
you can set Destination like below in which you can specify file name too.
setDestinationUri()
or
setDestinationInExternalPublicDir("/mnt/sdcard/", "test.jpg");
Related
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.
I have a DownloadManager to download video so that I can use player to play. But when I start to download, it has generated the unfinished .mp4 file, so when I tried to play before finish downloading, it will show error, is that possible change its extension before finish downloading?
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE).setDescription("caching video").setDestinationInExternalPublicDir("/Android/data/", id + ".mp4");
long id = manager.enqueue(request);
You can solve your problem in a little different way
Instead of change its extension before finish downloading you need to start the download with the changed extension like this:
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE).setDescription("caching video").setDestinationInExternalPublicDir("/Android/data/", id + ".mp4.tmp");
and once download is finished, rename file to "/Android/data/", id + ".mp4"
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
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...
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.