下载视频文件保存到本地
2015-11-28 18:06
417 查看
在Activity中或者Service中开启子线程:
由上面可以看出,要把文件保存到SD卡上面,分为下面几步:
1.首先创建一个目录或者说是路径,放到文件中,判断文件是否存在,存在就把网络上面的转换成流存到SD中,不存在,就在SD卡中创建这样的目录。
2.把网络链接URL,转换成流,并写入已经创建好的SD卡文件中。
3.获取存到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" />
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" />
相关文章推荐
- Linux系统硬盘读写测试
- Android逆向之旅---静态分析技术来破解Apk
- android生命周期:activity
- 第十三天-linux正则表达式及重点命令
- 【博弈-找规律】HDOJ 取石子游戏 2516
- scala里的静态代理(static-forwarders)
- 重启win10文件资源管理器
- Android兼容性问题 -- RelativeLayout.mesure()出现NullPointerException异常
- Qt 简介--Qt 类简介专题(一)
- 杭电1591 hdu 1591 Encoded Love-letter
- Qt Object 类简介--Qt 类简介专题(二)
- 奇怪的剪贴板
- bean
- scala:函数即对象
- 女性其实最需要三种蔬菜!赶紧看看哟~~~
- AVL树的实现
- Qt QWidget 类简介--Qt 类简介专题(三)
- bower初接触
- ztree+java后台取数据(包括异步)生成树状图
- Big Number