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);
? ? ? ? ? ? // 打開連接
? ? ? ? ? ? URLConnection conn = url.openConnection();
? ? ? ? ? ? // 打開輸入流
? ? ? ? ? ? InputStream is = conn.getInputStream();
? ? ? ? ? ? // 創(chuàng)建字節(jié)流
? ? ? ? ? ? byte[] bs =new byte[1024];
? ? ? ? ? ? int len;
? ? ? ? ? ? OutputStream os =new FileOutputStream(fileName);
? ? ? ? ? ? // 寫數(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());
? ? ? ? }
}