下載視頻到本地相冊(cè)

private void downloadRecord() {

// 下載地址

? ? ? ? String path =mItemList.get(mSharePosition).getAddressUrl();

? ? ? ? // 創(chuàng)建文件夾,在存儲(chǔ)卡下

//? ? ? ? String dirName = Environment.getExternalStorageDirectory() + "/" + mContext.getPackageName();

///storage/emulated/0/DCIM/com.dianchou.dcw

//? ? ? ? String dirName =? Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+"/" + mContext.getPackageName();

///storage/emulated/0/DCIM/Camera

? ? ? ? String dirName =? Environment.getExternalStorageDirectory().getPath() + File.separator +"DCIM"+ File.separator+"Camera";

? ? ? ? File file =new File(dirName);

? ? ? ? // 文件夾不存在時(shí)創(chuàng)建

? ? ? ? if (!file.exists()) {

file.mkdir();

? ? ? ? }

// 下載后的文件名

? ? ? ? int i = path.lastIndexOf("/"); // 取的最后一個(gè)斜杠后的字符串為名

? ? ? ? ///storage/emulated/0/DCIM/com.dianchou.dcw/20180907_luZym8By__4ENd9u0c6ZvOzXTzdR.mp4

? ? ? ? String fileName = dirName + path.substring(i, path.length());

? ? ? ? File file1 =new File(fileName);

? ? ? ? if (file1.exists()) {

// 如果已經(jīng)存在, 就不下載了, 去播放

//? ? ? ? ? ? startVideo(fileName);

? ? ? ? }else {

new Thread(new Runnable() {

@Override

? ? ? ? ? ? ? ? public void run() {

? ? ? ? ? ? ? ? ? ? DOWNLOAD(fileName);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }).start();

? ? ? ? }

}

// 下載具體操作

? ? private void DOWNLOAD(String fileName) {

String path =mItemList.get(mSharePosition).getAddressUrl();

? ? ? ? try {

URL url =new URL(path);

? ? ? ? ? ? // 打開(kāi)連接

? ? ? ? ? ? URLConnection conn = url.openConnection();

? ? ? ? ? ? // 打開(kāi)輸入流

? ? ? ? ? ? InputStream is = conn.getInputStream();

? ? ? ? ? ? // 創(chuàng)建字節(jié)流

? ? ? ? ? ? byte[] bs =new byte[1024];

? ? ? ? ? ? int len;

? ? ? ? ? ? OutputStream os =new FileOutputStream(fileName);

? ? ? ? ? ? // 寫(xiě)數(shù)據(jù)

? ? ? ? ? ? while ((len = is.read(bs)) != -1) {

os.write(bs, 0, len);

? ? ? ? ? ? }

// 完成后關(guān)閉流

//? ? ? ? ? ? Log.e(TAG, "download-finish");

? ? ? ? ? ? os.close();

? ? ? ? ? ? is.close();

? ? ? ? ? ? Intent intent =new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

? ? ? ? ? ? intent.setData(Uri.fromFile(new File(fileName)));

? ? ? ? ? ? mContext.sendBroadcast(intent);

? ? ? ? ? ? mContext.runOnUiThread(new Runnable() {

@Override

? ? ? ? ? ? ? ? public void run() {

ToastUtils.showNormalShort(mContext,"下載成功");

? ? ? ? ? ? ? ? }

});

? ? ? ? ? ? //? ? ? ? ? ? }

? ? ? ? }catch (Exception e) {

e.printStackTrace();

//? ? ? ? ? ? Log.e(TAG, "e.getMessage() --- " + e.getMessage());

? ? ? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容