Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I download a video file to SD card?

I have a video file(.MP4 format) and I want to allow the user to be able to download the video to their SD card.I currently use this code but its not working..

String PATHSdcard = "/sdcard/Video/";  

 public void DownloadFromUrl(String VideoURL, String fileName)         


try { URL url = new URL("https://javmed-prod.s3.amazonaws.com/666034cbe81045f2a2da50e5205e376b.mp4");
             File file = new File(fileName);

             long sTime = System.currentTimeMillis();
             URLConnection URLcon = url.openConnection();

             InputStream is = URLcon.getInputStream();
             BufferedInputStream bis = new BufferedInputStream(is);

             ByteArrayBuffer baf = new ByteArrayBuffer(50);
             int current = 0;
             while ((current = bis.read()) != -1) {
                     baf.append((byte) current);
             }

             FileOutputStream fos = new FileOutputStream(PATHSdcard+file);
             fos.write(baf.toByteArray());
             fos.close();

     } catch (IOException e) {
             Log.d("ERROR.......",e+"");
     }

thanks for any help!

like image 849
Chirag Patel Avatar asked Oct 19 '11 05:10

Chirag Patel


People also ask

Can you move videos to SD card?

You can move or copy files to an SD card on your device. Files by Google works on Android version 5.0 and up. If you don't have the app, you can download it from the Play Store.

Why won't my videos save to my SD card?

Clear Cache of Your Camera App And Gallery App To fix Camera not Saving to SD Card. Another way you can fix the camera not saving to SD card is by clearing your camera's app cache. Every app on your Android device has a cache which is part of the internal storage of your device that saves files temporarily.


1 Answers

Try this Solution

package com.Video.ALLTestProject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;


public class VideoSaveSDCARD extends Activity{

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ProgressBack PB = new ProgressBack();
        PB.execute("");
    }

    private class ProgressBack extends AsyncTask<String,String,String> {
         ProgressDialog PD;
        @Override
        protected void onPreExecute() {
            PD= ProgressDialog.show(LoginPage.this,null, "Please Wait ...", true);
            PD.setCancelable(true);
        }

        @Override
        protected void doInBackground(String... arg0) {
        downloadFile("http://beta-vidizmo.com/hilton.mp4","Sample.mp4");            

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

}
like image 75
Chirag Patel Avatar answered Sep 26 '22 17:09

Chirag Patel