下载视频文件保存到本地


在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" />




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值