基础库
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

114 rindas
3.5 KiB

  1. import 'dart:collection';
  2. import 'dart:io';
  3. import 'package:dio/dio.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:fluttertoast/fluttertoast.dart';
  6. import 'package:path_provider/path_provider.dart';
  7. import 'package:permission_handler/permission_handler.dart';
  8. import 'package:save_image/save_image.dart';
  9. class Download {
  10. static HashMap hashMap = HashMap();
  11. Future<String> fileToGallery(
  12. String url, {
  13. String postfix,
  14. Function(int, int) progress,
  15. }) async {
  16. // 检查是否已有读写内存的权限
  17. bool status = await Permission.storage.isGranted;
  18. //判断如果还没拥有读写权限就申请获取权限
  19. if (status || await Permission.storage.request().isGranted) {
  20. if (hashMap.containsKey(url)) {
  21. return "正在下载";
  22. } else {
  23. hashMap[url] = false;
  24. }
  25. try {
  26. var strs = url.split('.');
  27. if (strs.length < 0) {
  28. return "文件链接错误";
  29. }
  30. var appDocDir = await getTemporaryDirectory();
  31. String savePath = appDocDir.path +
  32. "/" +
  33. DateTime.now().toIso8601String().toString() +
  34. (strs[strs.length - 1].length > 4
  35. ? (postfix ?? "")
  36. : ('.' + strs[strs.length - 1]));
  37. await Dio().download(url, savePath, onReceiveProgress: (count, toal) {
  38. if (progress != null) {
  39. progress(count, toal);
  40. }
  41. });
  42. print(savePath);
  43. var result = await SaveImage.save(imageBytes: File(savePath).readAsBytesSync());
  44. if (result != null && result) {
  45. return "保存成功";
  46. } else {
  47. return "保存失败";
  48. }
  49. } catch (e) {
  50. return "下载失败,原因:" + e.toString();
  51. } finally {
  52. hashMap.remove(url);
  53. }
  54. } else {
  55. return "请前往系统设置开启应用的内存读取权限再尝试";
  56. }
  57. }
  58. Future<String> videoFileToGallery(
  59. String url, {
  60. String postfix,
  61. Function(int, int) progress,
  62. }) async {
  63. // 检查是否已有读写内存的权限
  64. bool status = await Permission.storage.isGranted;
  65. //判断如果还没拥有读写权限就申请获取权限
  66. if (status || await Permission.storage.request().isGranted) {
  67. if (hashMap.containsKey(url)) {
  68. return "正在下载";
  69. } else {
  70. hashMap[url] = false;
  71. }
  72. try {
  73. var strs = url.split('.');
  74. if (strs.length < 0) {
  75. return "文件链接错误";
  76. }
  77. var appDocDir = await getTemporaryDirectory();
  78. String savePath = appDocDir.path +
  79. "/" +
  80. DateTime.now().toIso8601String().toString() +
  81. (strs[strs.length - 1].length > 4
  82. ? (postfix ?? "")
  83. : ('.' + strs[strs.length - 1]));
  84. await Dio().download(url, savePath, onReceiveProgress: (count, toal) {
  85. if (progress != null) {
  86. progress(count, toal);
  87. }
  88. });
  89. print(savePath);
  90. //视屏文件插入到手机相册中的方法
  91. var result = await SaveImage.saveFile(savePath);
  92. // var result = await SaveImage.save(imageBytes: File(savePath).readAsBytesSync());
  93. if (result != null && result) {
  94. return "保存成功";
  95. } else {
  96. return "保存失败";
  97. }
  98. } catch (e) {
  99. return "下载失败,原因:" + e.toString();
  100. } finally {
  101. hashMap.remove(url);
  102. }
  103. } else {
  104. return "请前往系统设置开启应用的内存读取权限再尝试";
  105. }
  106. }
  107. }