2013-06-24 160 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