Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download video from URL?

I am trying to download a video file from URL. Below is my Code.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ProgressBack PB = new ProgressBack();
        PB.execute("");
    }

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

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

        @Override
        protected String doInBackground(String... arg0) {
            downloadFile("https://r8---sn-nhpax-ua8z.googlevideo.com/videoplayback?c=web&clen=17641691&cpn=Mf_hDzzzBYPH8N_J&cver=as3&dur=189.857&expire=1425270280&fexp=905657%2C907263%2C912333%2C926419%2C927622%2C931358%2C934947%2C936928%2C9406255%2C9406746%2C9406850%2C943917%2C945093%2C947225%2C947240%2C948124%2C951703%2C952302%2C952605%2C952612%2C952620%2C952901%2C955301%2C957201%2C959701&gcr=il&gir=yes&id=o-AM54E58Im9m8yqaerEsKkGXOx0IWge8YN4h6OhFkcDTe&initcwndbps=1488750&ip=84.228.53.86&ipbits=0&itag=135&keepalive=yes&key=yt5&lmt=1402678222642477&mime=video%2Fmp4&mm=31&ms=au&mt=1425248654&mv=m&pl=20&ratebypass=yes&requiressl=yes&signature=E8027BCB4C1EE76254FC008B0044655E58485D81.931863F3A7AD6C6B01262BCD723B37E5396D4317&source=youtube&sparams=clen%2Cdur%2Cgcr%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cupn%2Cexpire&sver=3&upn=moGJHdfD4Z8", "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());
        }
    }
}

But it is not downloading. and it is showing java.io.FileNotFoundException. Is there any other way to download video file or anything wrong in my code. Can someone please help me?

like image 630
Swati Jain Avatar asked Jan 30 '17 12:01

Swati Jain


1 Answers

    //Check if External Storage permission js allowed
    if (!storageAllowed()) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(getActivity(), Constant.PERMISSIONS_STORAGE, Constant.REQUEST_EXTERNAL_STORAGE);
        progressDialog.dismiss();
        showToast("Kindly grant the request and try again");
    }else {

        String mBaseFolderPath = android.os.Environment
                .getExternalStorageDirectory()
                + File.separator
                + "FolderName" + File.separator;
        if (!new File(mBaseFolderPath).exists()) {
            new File(mBaseFolderPath).mkdir();
        }

        if (downloadUrl == null || TextUtils.isEmpty(downloadUrl)) {
            showToast("Download url not found!");
            return;
        }

        Uri downloadUri = Uri.parse(url.trim());
        if (downloadUri == null) {
            showToast("Download url not found!");
            return;
        }

        String mFilePath = "file://" + mBaseFolderPath + "/" + fname ;
        DownloadManager.Request req = new DownloadManager.Request(downloadUri);
        req.setDestinationUri(Uri.parse(mFilePath));
        req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        DownloadManager dm = (DownloadManager) getActivity().getSystemService(getActivity().DOWNLOAD_SERVICE);
        dm.enqueue(req);
        progressDialog.dismiss();
        loadInterstitialAd();
    }
}
like image 65
Saud Kamran Avatar answered Nov 09 '22 14:11

Saud Kamran