From 152c55c9cf1f07c30eb839e43348ba799fa0bd4b Mon Sep 17 00:00:00 2001 From: "23028876916@qq.com" Date: Mon, 10 May 2021 11:39:38 +0800 Subject: [PATCH] =?UTF-8?q?0510=20=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=88=B0=E7=9B=B8=E5=86=8C=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/util/download/download.dart | 55 ++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/lib/util/download/download.dart b/lib/util/download/download.dart index a59d3ce..7af0dbc 100644 --- a/lib/util/download/download.dart +++ b/lib/util/download/download.dart @@ -35,7 +35,9 @@ class Download { String savePath = appDocDir.path + "/" + DateTime.now().toIso8601String().toString() + - (strs[strs.length - 1].length > 4 ? (postfix ?? "") : ('.' + strs[strs.length - 1])); + (strs[strs.length - 1].length > 4 + ? (postfix ?? "") + : ('.' + strs[strs.length - 1])); await Dio().download(url, savePath, onReceiveProgress: (count, toal) { if (progress != null) { progress(count, toal); @@ -57,4 +59,55 @@ class Download { return "请前往系统设置开启应用的内存读取权限再尝试"; } } + + Future videoFileToGallery( + String url, { + String postfix, + Function(int, int) progress, + }) async { + // 检查是否已有读写内存的权限 + bool status = await Permission.storage.isGranted; + + //判断如果还没拥有读写权限就申请获取权限 + if (status || await Permission.storage.request().isGranted) { + if (hashMap.containsKey(url)) { + return "正在下载"; + } else { + hashMap[url] = false; + } + try { + var strs = url.split('.'); + if (strs.length < 0) { + return "文件链接错误"; + } + var appDocDir = await getTemporaryDirectory(); + String savePath = appDocDir.path + + "/" + + DateTime.now().toIso8601String().toString() + + (strs[strs.length - 1].length > 4 + ? (postfix ?? "") + : ('.' + strs[strs.length - 1])); + await Dio().download(url, savePath, onReceiveProgress: (count, toal) { + if (progress != null) { + progress(count, toal); + } + }); + print(savePath); + //视屏文件插入到手机相册中的方法 + var result = await SaveImage.saveFile(savePath); + // var result = await SaveImage.save(imageBytes: File(savePath).readAsBytesSync()); + if (result != null && result) { + return "保存成功"; + } else { + return "保存失败"; + } + } catch (e) { + return "下载失败,原因:" + e.toString(); + } finally { + hashMap.remove(url); + } + } else { + return "请前往系统设置开启应用的内存读取权限再尝试"; + } + } }