2017-01-30 2 views
1

Я пытаюсь загрузить видеофайл с URL-адреса. Ниже мой код.Как скачать видео с URL-адреса?

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

Но он не загружается. и он показывает java.io.FileNotFoundException. Есть ли другой способ загрузить видеофайл или что-то не так в моем коде. Может кто-нибудь, пожалуйста, помогите мне?

+0

Вы можете использовать класс Download Manager для загрузки – Akash

+0

возможного дубликата http://stackoverflow.com/questions/3692252/android-how-do-i-download-a- video-file-to-sd-card? rq = 1 –

ответ

0

попробовать это:

private static void downloadFile(String url, File outputFile) { 
try { 
    URL u = new URL(url); 
    URLConnection conn = u.openConnection(); 
    int contentLength = conn.getContentLength(); 

    DataInputStream stream = new DataInputStream(u.openStream()); 

    byte[] buffer = new byte[contentLength]; 
    stream.readFully(buffer); 
    stream.close(); 

    DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile)); 
    fos.write(buffer); 
    fos.flush(); 
    fos.close(); 
} catch(FileNotFoundException e) { 
    return; // swallow a 404 
} catch (IOException e) { 
    return; // swallow a 404 
} 
} 
+0

Что мне нужно передать в параметре для ** File outputFile **? –

+0

Ваше имя файла Sample.mp4, попробуйте это. –

+0

Спасибо за ваш ответ. Я пробовал ваш код, но не работал. –

Смежные вопросы