假设我要下载这个文件(随机文件): http://www.analysis.im/uploads/seminar/pdf-sample.pdf
这是我的代码:
DownloadManager.Request req = new DownloadManager.Request(Uri.parse("http://www.analysis.im/uploads/seminar/pdf-sample.pdf"));
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Random title")
.setDescription("Random description")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "pdf-sample.pdf");
在 Debug模式下,我可以看到所有参数都是正确的,为什么下载没有开始?
编辑
我当前的权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
最佳答案
你允许在DownloadManager.Request.NETWORK_MOBILE
的网络类型下下载,但为什么要设置setAllowedOverRoaming(false)
?
我尝试使用 Downloadmanager
下载文件,这是我的代码:
String url = "http://example.com/large.zip";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
// only download via WIFI
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setTitle("Example");
request.setDescription("Downloading a very large zip");
// we just want to download silently
request.setVisibleInDownloadsUi(false);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setDestinationInExternalFilesDir(context, null, "large.zip");
// enqueue this request
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
downloadID = downloadManager.enqueue(request);
希望您有所启发。
关于android - DownloadManager 没有开始下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30299956/