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
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)
setDestinationInExternalPublicDir()
and related methods let you specify the output directory as a File
, not as an OutputStream
. –
Peart .setDestinationUri(dst_uri);
but it gives error Caused by: java.lang.IllegalArgumentException: Not a file URI: –
Respecting DownloadManager
cannot write to your app's internal storage (e.g., getDatabasePath()
). Beyond that, whatever dst_uri
is does not have the file://
scheme, apparently. –
Peart Environment.getExternalStoragePublicDirectory(dst_uri.getPath()).mkdirs();
request.setDestinationInExternalPublicDir(dst_uri.getPath(), "file name with . ext" );
–
Respecting getExternalStoragePublicDirectory()
does not take a path as a parameter. –
Peart 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");
Environment.getExternalStorageDirectory()
instead of hardcoding to "/mnt/sdcard" –
Vast 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:
pause
and resume
the download in download manager
–
Senaidasenalda © 2022 - 2024 — McMap. All rights reserved.