我想我有一个相当简单的问题。
http://www.vogella.com/blog/2011/06/14/android-downloadmanager-example/
我一直在按照上面链接中的教程学习。我该如何更改下载文件的文件路径?
提前感谢。
我想我有一个相当简单的问题。
http://www.vogella.com/blog/2011/06/14/android-downloadmanager-example/
我一直在按照上面链接中的教程学习。您可以使用DownloadManager.Request对象配置此类信息。在本教程中,Request
对象在onClick()
中被创建和使用。
例如:
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");
(以上代码来自此示例项目)
CommonsWare的回答中最后一行指出了目的地。他只是使用SD卡上的常规下载文件夹,但你也可以这样做:
req.setDestinationInExternalPublicDir("/mnt/sdcard/Myfolder", "file_name.extension");
Environment.getExternalStorageDirectory()
替代硬编码的 **"/mnt/sdcard"**。 - KalaraniOriginal Answer
翻译成“最初的回答”。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);
您可以尝试以下操作:
setDestinationInExternalPublicDir()
及其相关方法允许您将输出目录指定为File
,而不是OutputStream
。 - CommonsWare.setDestinationUri(dst_uri);
但是它会出现错误**Caused by: java.lang.IllegalArgumentException: Not a file URI:**。 - mehmetgetExternalStoragePublicDirectory()
不接受路径作为参数。 - CommonsWare