private void downloadRecord() {
// 下载地址
String path =mItemList.get(mSharePosition).getAddressUrl();
// 创建文件夹,在存储卡下
// 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);
// 文件夹不存在时创建
if (!file.exists()) {
file.mkdir();
}
// 下载后的文件名
int i = path.lastIndexOf("/"); // 取的最后一个斜杠后的字符串为名
///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()) {
// 如果已经存在, 就不下载了, 去播放
// 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();
// 创建字节流
byte[] bs =new byte[1024];
int len;
OutputStream os =new FileOutputStream(fileName);
// 写数据
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完成后关闭流
// 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());
}
}