【问题标题】:Download video from Firebase storage by URL通过 URL 从 Firebase 存储中下载视频
【发布时间】:2018-07-09 22:50:13
【问题描述】:

我将视频上传到 Firebase 存储。 我有视频的网址。 在我的应用程序中,我有一个下载按钮。 我只想将视频下载到移动存储中。

这是网址

String videoUrl = "https://firebasestorage.googleapis.com/v0/b/whatsappstatus-b23a0.appspot.com/o/videos%2F__-1__2__content_____media__external__video__media__123759__ORIGINAL__NONE__1830167854?alt=media&token=ff6db9ff-dd83-44f0-9beb-4578455abd3f"

oncreate方法中,我在调用

      new ProgressBack().execute("");

这就是我所做的

   class ProgressBack extends AsyncTask<String,String,String> {
        ProgressDialog PD;

        @Override
        protected void onPreExecute() {
            PD = ProgressDialog.show(VideoDetailActivity.this, null, "Please Wait ...", true);
            PD.setCancelable(true);
        }

        @Override
        protected String doInBackground(String... arg0) {
            downloadFile(videoUrl, "Sample.mp4");

            return null;
        }

        protected void onPostExecute(Boolean result) {
            PD.dismiss();

        }
    }


   private void downloadFile(String fileURL, String fileName) {
    try {
        String rootDir = Environment.getExternalStorageDirectory()
                + File.separator + "Video";
        File rootFile = new File(rootDir);
        rootFile.mkdir();
        URL url = new URL(fileURL);
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();
        FileOutputStream f = new FileOutputStream(new File(rootFile,
                fileName));
        InputStream in = c.getInputStream();
        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = in.read(buffer)) > 0) {
            f.write(buffer, 0, len1);
        }
        f.close();
    } catch (IOException e) {
        Log.d("Error....", e.toString());
    }
}

我不知道我在这里缺少什么。

【问题讨论】:

  • 您好,只需运行您的 downloadFile 方法作为独立下载文件。如果仍然有问题 -> stackoverflow.com/a/3028660/3468297 应该会有所帮助。
  • 谢谢,我会看看这个。
  • 我发现我的 downloadFile 方法有问题。我使用了您提到的 downloadManager,它完成了这项工作。非常感谢!

标签: java android firebase firebase-storage


【解决方案1】:

感谢@OliverTester

我使用了 android downloadManager,它完全按照我想要的方式完成了这项工作。

我将视频 url 传递给此方法并下载它。令人惊讶的是,这有两行代码。我不必处理 asynctask 之类的东西。

private void downloadManager(String url) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setDescription("download");
        request.setTitle(""+songtitle);
// in order for this if to run, you must use the android 3.2 to compile your app
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, ""+songtitle+".mp4");

// get download service and enqueue file
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);
    }

【讨论】:

  • 如何查看下载是否完成?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 2018-08-09
  • 2019-09-29
  • 2017-06-22
  • 2020-01-14
  • 2018-12-08
  • 2018-07-04
相关资源
最近更新 更多