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
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 have used Android Download Manager for download Files from server as it's handles network error and resume downloading.
It's working fine, I can download files using this,
Problem : When I start downloading it's complete successfully and file is also downloaded once but it Store/Show the downloaded files in two directories one is default download directory of android phone and second is custom directory which created by me as i only want to Store/Show in my custom directory and then want to read only from that directory files.
Question : how i only Store/Show downloaded files in custom created directory ??
Below is the code which i used to download files using Download Manager.
public void downloadWithManager(String urlPath) {
Long tsLong = System.currentTimeMillis() / 1000;
File direct = new File(Environment.getExternalStorageDirectory() + "/Demo");
if (!direct.exists()) {
direct.mkdirs();
}
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(urlPath);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
//Restrict the types of networks over which this download may proceed.
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
//Set whether this download may proceed over a roaming connection.
request.setAllowedOverRoaming(false);
//Set the title of this download, to be displayed in notifications (if enabled).
request.setTitle(tsLong.toString() + ".mp4");
//Set a description of this download, to be displayed in notifications (if enabled)
request.setDescription("Download in progress...");
//Set the custom local destination for the downloaded file
request.setDestinationInExternalPublicDir("/Demo", tsLong.toString() + ".mp4");
//Enqueue a new download and same the referenceId
downloadReference = downloadManager.enqueue(request);
}
Thank you.
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");
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.