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.
Related
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 using Download Manager
and i want to download some audio files using my own webview.
I use the following code:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url2));
request.setDescription("Downloading");
request.setTitle("File :");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "audio.mp3");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Even if the the File Name is audio.mp3 download manager does not recongise the file type.
ScreenShot explains the main problem.
Using other apps like default browser downloaded files look like this!
The solution is simple:
Just add the following line
request.setMimeType("audio/MP3");
More info here
not work : request.setMimeType("audio/MP3");
To operate, follow the steps below.
request.setMimeType("audio/mpeg");
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.
Hi have simple app in which data has been uploaded from the web server I am using .net web services. I need to replace the document on same location, so that duplicate document not place on same folder.
I am using:
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS + "/Downloads",
name );
is it right approach? can I delete document from physical place and then replace it with new one. if yes How this thing is possible.
this is my download manager and I am trying to delete name document and then trying to replace with new one. Please suggest. thanks
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(name);
// 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(
Environment.DIRECTORY_DOWNLOADS + "/Downloads", name);
DownloadManager manager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
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...