您的位置:首页 > 其它

下载视频文件保存到本地

2015-11-28 18:06 417 查看
在Activity中或者Service中开启子线程:

private void playvideo() {
if (!URLUtil.isNetworkUrl(this.remoteUrl)) {
mVideoView.setVideoPath(this.remoteUrl);
mVideoView.start();
return;
}

showProgressDialog();  // 显示进度条

new Thread(new Runnable() {

@Override
public void run() {
FileOutputStream out = null;
InputStream is = null;
try {
if (localUrl == null) {
localUrl = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ "/VideoCache/"
+ System.currentTimeMillis() + ".mp4";   // 创建本地文件的路径
}
System.out.println("获取本地SD卡的Url TWO: " + localUrl);

File cacheFile = new File(localUrl);
if (!cacheFile.exists()) {     // 如果缓存文件不存在,就创建
cacheFile.getParentFile().mkdirs();
cacheFile.createNewFile();
}

readSize = cacheFile.length();
System.out.println("缓存视频文件的长度" + readSize);
out = new FileOutputStream(cacheFile, true);

URL url = new URL(remoteUrl);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setRequestProperty("User-Agent", "NetFox");
httpConnection.setRequestProperty("RANGE", "bytes=" + readSize + "-");

is = httpConnection.getInputStream();

mediaLength = httpConnection.getContentLength();
if (mediaLength == -1) {
return;
}

mediaLength += readSize;

byte buf[] = new byte[4 * 1024];
int size = 0;
/**最后读取的大小*/
long lastReadSize = 0;

mHandler.sendEmptyMessage(VIDEO_STATE_UPDATE);

while ((size = is.read(buf)) != -1) {   //
try {
out.write(buf, 0, size);   // 把网络视频文件写入SD卡中
readSize += size;
} catch (Exception e) {
e.printStackTrace();
}

if (!isready) {
if ((readSize - lastReadSize) > READY_BUFF) {
lastReadSize = readSize;
mHandler.sendEmptyMessage(CACHE_VIDEO_READY);
}
} else {
if ((readSize - lastReadSize) > CACHE_BUFF * (errorCnt + 1)) {
lastReadSize = readSize;
mHandler.sendEmptyMessage(CACHE_VIDEO_UPDATE);
}
}
}

mHandler.sendEmptyMessage(CACHE_VIDEO_END);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}

if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}

}
}).start();

}

由上面可以看出,要把文件保存到SD卡上面,分为下面几步:

1.首先创建一个目录或者说是路径,放到文件中,判断文件是否存在,存在就把网络上面的转换成流存到SD中,不存在,就在SD卡中创建这样的目录。

String localUrl = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ "/VideoCache/"
+ System.currentTimeMillis() + ".mp4";   // 创建本地文件的路径

File cacheFile = new File(localUrl);
if (!cacheFile.exists()) {     // 如果文件不存在,就创建
cacheFile.getParentFile().mkdirs();
cacheFile.createNewFile();
}


2.把网络链接URL,转换成流,并写入已经创建好的SD卡文件中。

FileOutputStream out = new FileOutputStream(cacheFile, true);
URL url = new URL(remoteUrl); //参数就是网络链接
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setRequestProperty("User-Agent", "NetFox");
httpConnection.setRequestProperty("RANGE", "bytes=" + readSize + "-");
InputStream is = httpConnection.getInputStream();		// 把网络连接转换成输入流

byte buf[] = new byte[4 * 1024];
int size = 0;

mHandler.sendEmptyMessage(VIDEO_STATE_UPDATE);

while ((size = is.read(buf)) != -1) {   //
try {
out.write(buf, 0, size);   // 把网络视频文件写入SD卡中
readSize += size;
} catch (Exception e) {
e.printStackTrace();
}
}

if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}

if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}

3.获取存到SD卡中的文件

String localUrl = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/VideoCache/"
+ System.currentTimeMillis() + ".mp4";   // 获取本地SD卡文件的路径

4.SD卡的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: