由网友(帅哥的立軆纹身)分享简介:我在.MP4格式的网站上的视频文件,我想,让用户能够通过单击链接来下载视频到自己的SD卡。有一个简单的方法来做到这一点。目前,我有这个code,但它不工作...不知道我做错了。感谢您的帮助!进口java.io.BufferedInputStream中;进口的java.io.File;进口java.io.FileOu...
我在.MP4格式的网站上的视频文件,我想,让用户能够通过单击链接来下载视频到自己的SD卡。有一个简单的方法来做到这一点。目前,我有这个code,但它不工作...不知道我做错了。感谢您的帮助!
进口java.io.BufferedInputStream中;
进口的java.io.File;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口的java.net.URL;
进口java.net.URLConnection中;
进口org.apache.http.util.ByteArrayBuffer;
进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
公共类视频管理延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);}
私人最终字符串PATH =/ SD卡/下载/; //把下载的文件在这里
公共无效DownloadFromUrl(字符串VideoURL,字符串文件名){//这是下载方法
尝试 {
网址URL =新的URL(http://www.ericmoyer.com/episode1.mp4); //你可以在这里写任何链接
档案文件=新的文件(文件名);
长的startTime = System.currentTimeMillis的();
Log.d(视频管理,下载开始时);
Log.d(视频管理,下载网址:+网址);
Log.d(视频管理,已下载文件的名称:+文件名);
/ *打开到URL的连接。 * /
URLConnection的UCON = url.openConnection();
/ *
*定义InputStreams从URLConnection的阅读。
* /
InputStream的是= ucon.getInputStream();
的BufferedInputStream双=新的BufferedInputStream(是);
/ *
*读取字节的缓冲区,直到有没有更多阅读(-1)。
* /
ByteArrayBuffer BAF =新ByteArrayBuffer(50);
INT电流= 0;
而((电流= bis.read())!= - 1){
baf.append((字节)电流);
}
/ *读取转换为字符串的字节数。 * /
FileOutputStream中FOS =新的FileOutputStream(路径+文件);
fos.write(baf.toByteArray());
fos.close();
Log.d(视频管理,下载现成的
+((System.currentTimeMillis的() - 的startTime)/ 1000)
+秒);
}赶上(IOException异常E){
Log.d(视频管理,错误:+ E);
}
}
}
解决方案
不运行内存? 我想一个视频文件非常大 - 这你写入文件之前,缓冲
。我知道你的榜样code是所有在互联网上 - 但它的坏下载! 使用这样的:
私人最终诠释TIMEOUT_CONNECTION = 5000; // 5秒
私人最终诠释TIMEOUT_SOCKET = 30000; // 30秒
网址URL =新的URL(IMAGEURL);
长的startTime = System.currentTimeMillis的();
Log.i(TAG,图片下载开头:+ IMAGEURL);
//打开到URL的连接。
URLConnection的UCON = url.openConnection();
//超时会影响需要多长时间的应用程序,以实现有一个连接问题
ucon.setReadTimeout(TIMEOUT_CONNECTION);
ucon.setConnectTimeout(TIMEOUT_SOCKET);
//定义InputStreams从URLConnection的阅读。
//使用3KB下载缓冲区
InputStream的是= ucon.getInputStream();
的BufferedInputStream inStream中=新的BufferedInputStream(是,1024 * 5);
FileOutputStream中outStream =新的FileOutputStream(文件);
byte []的BUFF =新的字节[5 * 1024];
//读取字节(并存储它们),直到有没有更多阅读(-1)
INT LEN;
而((LEN = inStream.read(BUFF))!= -1)
{
outStream.write(BUFF,0,len个);
}
//清理
outStream.flush();
outStream.close();
inStream.close();
Log.i(TAG,下载完成
+((System.currentTimeMillis的() - 的startTime)/ 1000)
+秒); 5
I have a video file on a website in .MP4 format and I want to allow the user to be able to download the video to their SD card by clicking a link. Is there an easy way to do this. I currently have this code but its not working...not sure what I am doing wrong. THanks for any help!
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);
}
}
}
解决方案
aren't running out of memory ? I imagine a video file is very large - which you are buffering before writing to file.
I know your example code is all over the internet - but it's BAD for downloading ! Use this:
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
相关推荐
最新文章