2010-09-11 111 views
16

我有.MP4格式的網站上的視頻文件,我想,讓用戶能夠通過點擊鏈接來下載視頻到他們的SD卡。是否有捷徑可尋。我目前有這個代碼,但它不工作...不知道我做錯了什麼。謝謝你的幫助!ANDROID:如何將視頻文件下載到SD卡?

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import org.apache.http.util.ByteArrayBuffer; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 

public class VideoManager extends Activity { 
/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);} 



     private final String PATH = "/sdcard/download/"; //put the downloaded file here 


     public void DownloadFromUrl(String VideoURL, String fileName) { //this is the downloader method 
       try { 
         URL url = new URL("http://www.ericmoyer.com/episode1.mp4"); //you can write here any link 
         File file = new File(fileName); 

         long startTime = System.currentTimeMillis(); 
         Log.d("VideoManager", "download begining"); 
         Log.d("VideoManager", "download url:" + url); 
         Log.d("VideoManager", "downloaded file name:" + fileName); 
         /* Open a connection to that URL. */ 
         URLConnection ucon = url.openConnection(); 

         /* 
         * Define InputStreams to read from the URLConnection. 
         */ 
         InputStream is = ucon.getInputStream(); 
         BufferedInputStream bis = new BufferedInputStream(is); 

         /* 
         * Read bytes to the Buffer until there is nothing more to read(-1). 
         */ 
         ByteArrayBuffer baf = new ByteArrayBuffer(50); 
         int current = 0; 
         while ((current = bis.read()) != -1) { 
           baf.append((byte) current); 
         } 

         /* Convert the Bytes read to a String. */ 
         FileOutputStream fos = new FileOutputStream(PATH+file); 
         fos.write(baf.toByteArray()); 
         fos.close(); 
         Log.d("VideoManager", "download ready in" 
             + ((System.currentTimeMillis() - startTime)/1000) 
             + " sec"); 

       } catch (IOException e) { 
         Log.d("VideoManager", "Error: " + e); 
       } 

     } 
} 
+0

你確定路徑/ SD卡/下載/存在嗎?你可以通過adb shell創建它 – 2010-09-11 19:34:18

回答

35

不運行內存? 我想象一個視頻文件非常大 - 在寫入文件之前您正在緩衝。

我知道你的示例代碼是在互聯網上 - 但它是不好的下載! 使用這個:

private final int TIMEOUT_CONNECTION = 5000;//5sec 
private final int TIMEOUT_SOCKET = 30000;//30sec 


      URL url = new URL(imageURL); 
      long startTime = System.currentTimeMillis(); 
      Log.i(TAG, "image download beginning: "+imageURL); 

      //Open a connection to that URL. 
      URLConnection ucon = url.openConnection(); 

      //this timeout affects how long it takes for the app to realize there's a connection problem 
      ucon.setReadTimeout(TIMEOUT_CONNECTION); 
      ucon.setConnectTimeout(TIMEOUT_SOCKET); 


      //Define InputStreams to read from the URLConnection. 
      // uses 3KB download buffer 
      InputStream is = ucon.getInputStream(); 
      BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5); 
      FileOutputStream outStream = new FileOutputStream(file); 
      byte[] buff = new byte[5 * 1024]; 

      //Read bytes (and store them) until there is nothing more to read(-1) 
      int len; 
      while ((len = inStream.read(buff)) != -1) 
      { 
       outStream.write(buff,0,len); 
      } 

      //clean up 
      outStream.flush(); 
      outStream.close(); 
      inStream.close(); 

      Log.i(TAG, "download completed in " 
        + ((System.currentTimeMillis() - startTime)/1000) 
        + " sec");5 
+2

增加了'outStream.flush();'這解決了0字節文件的問題 – 2011-07-19 03:37:47

+1

是否可以播放當前正在下載的視頻?意思是說讓one.mp4目前處於下載狀態,這樣在使用MediaPlayer下載時可以播放相同的內容嗎? – Scorpion 2012-10-16 18:27:18

+0

@SomeoneSomewhere我的視頻格式爲.swf?將使用您的代碼下載嗎? – Dhasneem 2013-06-14 09:26:10

23

切勿硬連線路徑,特別是外部存儲。許多設備上的路徑錯誤。使用Environment.getExternalStoragePath()獲得外部存儲的根(其中可以/sdcard/mnt/sdcard或別的東西)。

一定要創建子目錄,使用File對象你從Environment.getExternalStoragePath()回來。

最後,不要只說「但它不工作」。我們不知道你的情況下「但它不起作用」是什麼意思。沒有這些信息,很難幫助你。

+0

非常感謝。我會記得下一次。 – 2010-09-12 19:07:25