通过内置下载服务下载文件



我正试图在应用程序中从远程服务器下载一个文件。我不想为此修改自定义代码。我想通过内置的下载器(安卓已经内置)下载该文件。如何做到这一点?这是正确的选择吗:

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);

最新更新