|
@@ -1,20 +1,22 @@ |
|
|
|
|
|
|
|
|
import 'dart:collection'; |
|
|
import 'dart:collection'; |
|
|
|
|
|
import 'dart:io'; |
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart'; |
|
|
import 'package:dio/dio.dart'; |
|
|
import 'package:flutter/material.dart'; |
|
|
import 'package:flutter/material.dart'; |
|
|
import 'package:fluttertoast/fluttertoast.dart'; |
|
|
import 'package:fluttertoast/fluttertoast.dart'; |
|
|
import 'package:image_gallery_saver/image_gallery_saver.dart'; |
|
|
|
|
|
import 'package:path_provider/path_provider.dart'; |
|
|
import 'package:path_provider/path_provider.dart'; |
|
|
import 'package:permission_handler/permission_handler.dart'; |
|
|
import 'package:permission_handler/permission_handler.dart'; |
|
|
|
|
|
import 'package:save_image/save_image.dart'; |
|
|
|
|
|
|
|
|
class Download { |
|
|
class Download { |
|
|
static HashMap hashMap = HashMap(); |
|
|
static HashMap hashMap = HashMap(); |
|
|
|
|
|
|
|
|
Future<String> fileToGallery( |
|
|
Future<String> fileToGallery( |
|
|
String url, { |
|
|
|
|
|
String postfix, |
|
|
|
|
|
Function(int, int) progress, |
|
|
|
|
|
}) async { |
|
|
|
|
|
|
|
|
String url, { |
|
|
|
|
|
String postfix, |
|
|
|
|
|
Function(int, int) progress, |
|
|
|
|
|
}) async { |
|
|
// 检查是否已有读写内存的权限 |
|
|
// 检查是否已有读写内存的权限 |
|
|
bool status = await Permission.storage.isGranted; |
|
|
bool status = await Permission.storage.isGranted; |
|
|
|
|
|
|
|
@@ -33,14 +35,17 @@ class Download { |
|
|
var appDocDir = await getTemporaryDirectory(); |
|
|
var appDocDir = await getTemporaryDirectory(); |
|
|
String savePath = appDocDir.path + "/" + DateTime.now().toIso8601String().toString() + (strs[strs.length - 1].length > 4 ? (postfix ?? "") : strs[strs.length - 1]); |
|
|
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) { |
|
|
await Dio().download(url, savePath, onReceiveProgress: (count, toal) { |
|
|
if(progress!=null){ |
|
|
|
|
|
|
|
|
if (progress != null) { |
|
|
progress(count, toal); |
|
|
progress(count, toal); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
print(savePath); |
|
|
print(savePath); |
|
|
final result = await ImageGallerySaver.saveFile(savePath); |
|
|
|
|
|
print(result); |
|
|
|
|
|
return "保存成功"; |
|
|
|
|
|
|
|
|
var result = await SaveImage.save(imageBytes: File(savePath).readAsBytesSync()); |
|
|
|
|
|
if (result != null && result) { |
|
|
|
|
|
return "保存成功"; |
|
|
|
|
|
} else { |
|
|
|
|
|
return "保存失败"; |
|
|
|
|
|
} |
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
return "下载失败,原因:" + e.toString(); |
|
|
return "下载失败,原因:" + e.toString(); |
|
|
} finally { |
|
|
} finally { |
|
|