简体   繁体   中英

Download video from Firebase storage by URL

I uploaded video to Firebase storage. I have the URL for the video. In my app, I have a download button. I want to simply download the video to mobile storage.

Here is the URL

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"

In the oncreate method, I'm calling

      new ProgressBack().execute("");

This is what I have done

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

I don't know what I am missing here.

thanks to @OliverTester

I used android downloadManager which did the job exactly the way i wanted it.

i passed video url to this method and it downloaded it. Amazing how two lines of code this this. I didn't have to deal with asynctask and stuff.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM