从网址下载SDCard上的视频
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从网址下载SDCard上的视频相关的知识,希望对你有一定的参考价值。
我正在申请从网址上下载sdcard上的视频。我推荐这个link。但是此链接包含的URL为.mp4格式。我必须下载新闻提要视频。我的格式不是.mp4格式。它主要是.swf。我的网址是,
http://cdnapi.kaltura.com/index.php/kwidget/wid/_483511/uiconf_id/5590821/entry_id/0_cf39ej0c
我通过解析rss feed(新闻提要)来获取此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);
}
}
}
但我无法看到这个视频?
其实我正在尝试实现视频流。但我不知道该怎么做。
我的代码中有什么问题吗?
请提出您的建议以实现上述目标。
先感谢您!!
答案
首先检查文件是否在您给出的路径中下载..我觉得您的PATH值是错误的。您需要将路径指定为“/ mnt / sdcard / filename”.. Ur路径未指向文件而是指向一个文件夹..检查一下
另一答案
试试这个:
不要手动定义SD卡的路径,而是使用
String Path = Environment.getExternalStorageDirectory().getPath();
并在后面添加一个文件名,比如
String filename = Path + "/filename.mp4"
并将这些filename
字符串传递给你的FileOutputStream
s。
编辑:
而不是下载文件,使用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();
}
并且不要忘记在qazxsw poi中提及互联网许可:
androidmanifest.xml
以上是关于从网址下载SDCard上的视频的主要内容,如果未能解决你的问题,请参考以下文章
android-get nullpointerexception 尝试访问外部存储(sdcard)上的视频
Cordova:Android 上的 sdcard 访问不起作用