浏览代码

0510 添加视频文件下载并添加到相册的方法

tags/0.0.16+2
23028876916@qq.com 3 年前
父节点
当前提交
152c55c9cf
共有 1 个文件被更改,包括 54 次插入1 次删除
  1. +54
    -1
      lib/util/download/download.dart

+ 54
- 1
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<String> 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 "请前往系统设置开启应用的内存读取权限再尝试";
}
}
}

正在加载...
取消
保存