如何从服务器下载视频文件?

时间:2019-05-08 06:28:21

标签: java android asynchronous android-download-manager

我真的是android上的新手,我正在尝试创建一个简单的应用程序,以从服务器下载视频文件...我正在做研究,发现您可以使用downloadManager来使用它,它看起来很容易使用,但看起来该选项仅接受url作为参数,并且为了下载视频,我必须发送(发布)一些参数以访问文件,我也正在阅读我们可以使用异步功能,但我不确定什么是最好的方法。

谢谢!

我正在使用该网站作为参考; https://developer.android.com/reference/android/app/DownloadManager

1 个答案:

答案 0 :(得分:0)

您可以尝试此代码

DownloadManager.Request request = new DownloadManager.Request(Uri.parse("your_video_url"));
                                request.setDescription("download");
                                request.setTitle("your_file_name");

                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                                    request.allowScanningByMediaScanner();
                                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                }
                                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "your_file_name"+".mp4");
                                DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                                manager.enqueue(request);