下載視訊檔案儲存到本地
阿新 • • 發佈:2019-01-25
在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" />