【发布时间】:2012-06-06 00:16:03
【问题描述】:
我想我有一个相当简单的问题。
http://www.vogella.com/blog/2011/06/14/android-downloadmanager-example/
我一直在关注上述网址中的教程。
如何更改下载的文件路径?
提前致谢
【问题讨论】:
标签: android file path filepath download-manager
我想我有一个相当简单的问题。
http://www.vogella.com/blog/2011/06/14/android-downloadmanager-example/
我一直在关注上述网址中的教程。
如何更改下载的文件路径?
提前致谢
【问题讨论】:
标签: android file path filepath download-manager
您使用此类信息配置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");
(以上代码来自this sample project)
【讨论】:
setDestinationInExternalPublicDir() 和相关方法让您可以将输出目录指定为File,而不是OutputStream。
.setDestinationUri(dst_uri);,但它给出了错误 Caused by: java.lang.IllegalArgumentException: Not a file URI:
CommonsWare 答案的最后一行说明了目的地。他只是使用 sdcard 上的常规下载文件夹,但你也可以这样做:
req.setDestinationInExternalPublicDir("/mnt/sdcard/Myfolder", "file_name.extension");
【讨论】:
Environment.getExternalStorageDirectory()而不是硬编码到"/mnt/sdcard"
您确保用户允许您的应用访问外部存储空间。包含此代码以供下载
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);
【讨论】:
您可以尝试以下操作:
【讨论】:
pause和resumedownload manager下载