下载视频到本地相册

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());

        }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容