Pārlūkot izejas kodu

1.增加文件保存到相册的工具类

tags/0.0.2+16
杨华轩 pirms 3 gadiem
vecāks
revīzija
abec7b5e78
4 mainītis faili ar 66 papildinājumiem un 1 dzēšanām
  1. +53
    -0
      lib/util/download/download.dart
  2. +1
    -1
      lib/util/net_util.dart
  3. +1
    -0
      lib/zhiying_comm.dart
  4. +11
    -0
      pubspec.yaml

+ 53
- 0
lib/util/download/download.dart Parādīt failu

@@ -0,0 +1,53 @@
import 'dart:collection';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

class Download {
static HashMap hashMap = HashMap();

Future<String> fileToGallery(
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);
final result = await ImageGallerySaver.saveFile(savePath);
print(result);
return "保存成功";
} catch (e) {
return "下载失败,原因:" + e.toString();
} finally {
hashMap.remove(url);
}
} else {
return "请前往系统设置开启应用的内存读取权限再尝试";
}
}
}

+ 1
- 1
lib/util/net_util.dart Parādīt failu

@@ -55,7 +55,7 @@ class NetUtil {
if (_dio == null) {
var setting = await NativeUtil.getSetting();
String domain = setting['domain']; //'http://www.hairuyi.com/';
_config(domain, proxyUrl: '192.168.0.66:8866');
_config(domain, proxyUrl: '192.168.0.102:8866');
}
return _dio;
}


+ 1
- 0
lib/zhiying_comm.dart Parādīt failu

@@ -36,4 +36,5 @@ export 'package:cached_network_image/cached_network_image.dart';
export 'util/photo_preview/photo_preview.dart';
export 'models/base/base_change_notifier.dart';
export 'util/parse_util.dart';
export 'util/download/Download.dart';


+ 11
- 0
pubspec.yaml Parādīt failu

@@ -61,6 +61,17 @@ dependencies:
# fluwx_no_pay: 2.3.1
# loading 动画
loading_indicator: ^1.2.0
#权限授权
permission_handler:
git:
url: 'http://192.168.0.138:3000/FnuoOS_Flutter_Components/permission_handler.git'
ref: '0.0.1'
#文件夹路径管理
path_provider: ^1.4.0

#保存文件到相册
image_gallery_saver: ^1.5.0



dev_dependencies:


Notiek ielāde…
Atcelt
Saglabāt