我正在尝试在我的应用程序中从远程服务器下载文件。我不想为此修改自定义代码。我想通过内置下载器(android 内置)下载文件。怎么做?这是正确的选择吗:
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png"));
enqueue = dm.enqueue(request);
最佳答案
尝试这个代码,它确实有效......
DownloadManager mgr = (DownloadManager) context.getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png");
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/test_folder", "testimage");
mgr.enqueue(request);
关于android - 通过内置下载服务下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29204257/