2013-06-24 91 views
0

我有一個應用程序從url下載SD卡上的視頻。我提到這個link。但是這個鏈接包含的URL爲.mp4格式。我必須下載新聞視頻。我的格式不是.mp4格式。它將主要是.swf。 礦的網址,從url下載SD卡上的視頻

http://cdnapi.kaltura.com/index.php/kwidget/wid/_483511/uiconf_id/5590821/entry_id/0_cf39ej0c

我通過解析RSS訂閱(新聞供稿)獲得此URL。

我的代碼是:

public class MainActivity extends Activity { 
    public final String TAG = "MainActivity"; 
    private final String PATH = "/sdcard/downloadVideo/"; 
    private final int TIMEOUT_CONNECTION = 5000;// 5sec 
    private final int TIMEOUT_SOCKET = 300000;// 30sec 
    //public final String imageURL = "http://www.cbsnews.com/video/watch/?id=50149183n&tag=api"; 
    public final String imageURL = "http://cdnapi.kaltura.com/index.php/kwidget/wid/_483511/uiconf_id/5590821/entry_id/0_cf39ej0c"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     DownloadFromUrl(imageURL, PATH); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public void DownloadFromUrl(String VideoURL, String fileName) { // this is 
                    // the 
                    // downloader 
                    // method 
     try { 
      URL url = new URL(VideoURL); 
      long startTime = System.currentTimeMillis(); 
      Log.i(TAG, "image download beginning: " + VideoURL); 

      // 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(fileName); 
      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"); 

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

    } 
} 

但我無法得到這個視頻?

其實我試圖實現視頻流。但我不知道該怎麼做。

我在代碼中做錯了什麼?

請給出您的建議,以達到上述目的。

預先感謝您!

回答

0

首先檢查文件是否在你給出的路徑獲取下載..我覺得你的PATH值是錯的..ü需要給出路徑「到/ mnt/SD卡/文件名」 .. 烏爾路徑沒有指向文件,而不是其指向的文件夾 ..檢查過

+0

現在我得到的輸出爲「3秒內完成下載」。在/ mnt /中,該路徑也已創建。但是,當我在圖庫中查看時,我無法單獨找到該視頻。 – Dhasneem

+0

U可以在該路徑中看到視頻嗎?即,如果你已經將文件名作爲abc.mp4,那麼它出現在路徑/mnt/sdcard/abc.mp4中 – Sudarshan

+0

它不是.mp4格式na嗎?我們只提供.mp4格式?它會存在嗎? – Dhasneem

0

試試這個:

而不是定義來手動SD卡路徑,利用

String Path = Environment.getExternalStorageDirectory().getPath(); 

並添加一個文件名後面,像

String filename = Path + "/filename.mp4" 

這些filename字符串傳遞給你的FileOutputStream秒。

編輯:

沒有下載文件中,查看它直接使用VideoView,用這樣的代碼:

VideoView mVideoView; 
MediaController mcon; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.videoplay); 
    mVideoView = (VideoView) findViewById(R.id.videoview); 
    String videourl="http://cdnapi.kaltura.com/index.php/kwidget/wid/_483511/uiconf_id/5590821/entry_id/0_cf39ej0c"; 
    mcon.setMediaPlayer(mVideoView); // add this line 
    mcon = new MediaController(this); 
    mVideoView.setMediaController(mcon); 
    mVideoView.requestFocus(); 
    mVideoView.setVideoURI(Uri.parse(videourl)); 
    mcon.show(); 
    mVideoView.start(); 
} 

並且不要忘記提及互聯網許可,不得以androidmanifest.xml

<uses-permission android:name="android.permission.INTERNET" /> 
+0

它不是.mp4格式na?我們只提供.mp4格式?它會存在嗎? – Dhasneem

+0

好友,你應該使用源文件中指定的文件的相同擴展名..就像我想要下載detail.txt那麼我應該指定我的目標文件爲filename.txt .. –

+0

它存在於路徑中已經。但我無法找到在gallery.After這樣做之後,仍然相同 – Dhasneem