141 lines
3.8 KiB
Dart
141 lines
3.8 KiB
Dart
import 'dart:io';
|
||
import 'dart:typed_data';
|
||
|
||
import 'package:device_info_plus/device_info_plus.dart';
|
||
import 'package:dio/dio.dart';
|
||
import 'package:flutter_common/utils/toast_utils.dart';
|
||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||
import 'package:image_gallery_saver_plus/image_gallery_saver_plus.dart';
|
||
|
||
// import 'package:image_gallery_saver/image_gallery_saver.dart';
|
||
import 'package:permission_handler/permission_handler.dart';
|
||
|
||
class DownLoadImageTool {
|
||
// 请求照片库权限
|
||
static Future<bool> requestPhotoPermission() async {
|
||
PermissionStatus status;
|
||
if (Platform.isIOS) {
|
||
status = await Permission.photosAddOnly.request();
|
||
} else {
|
||
status = await Permission.photos.request();
|
||
}
|
||
|
||
if (status.isPermanentlyDenied) {
|
||
await openAppSettings();
|
||
}
|
||
|
||
return status.isGranted || status.isLimited;
|
||
}
|
||
|
||
// 或者请求存储权限(适用于Android)
|
||
static Future<bool> requestStoragePermission() async {
|
||
if (Platform.isIOS) {
|
||
return requestPhotoPermission();
|
||
}
|
||
|
||
if (Platform.isAndroid) {
|
||
// 对于Android 13及以上版本
|
||
if (await DeviceInfoPlugin()
|
||
.androidInfo
|
||
.then((info) => info.version.sdkInt) >=
|
||
33) {
|
||
var status = await Permission.photos.request();
|
||
if (status.isPermanentlyDenied) {
|
||
await openAppSettings();
|
||
}
|
||
return status.isGranted || status.isLimited;
|
||
} else {
|
||
// 对于Android 13以下版本
|
||
var status = await Permission.storage.request();
|
||
if (status.isPermanentlyDenied) {
|
||
await openAppSettings();
|
||
}
|
||
return status.isGranted;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
///保存到相册
|
||
static Future<dynamic> savePhoto({
|
||
required String imageUrl,
|
||
bool? isHideLoading,
|
||
}) async {
|
||
//获取保存相册权限,如果没有,则申请改权限
|
||
bool permition = await requestStoragePermission();
|
||
if (permition) {
|
||
var result = imageRequest(
|
||
imageUrl: imageUrl,
|
||
isHideLoading: isHideLoading,
|
||
);
|
||
return result;
|
||
} else {
|
||
//重新请求--第一次请求权限时,保存方法不会走,需要重新调一次
|
||
ToastUtils.showToast(msg: '请打开手机相册权限');
|
||
// savePhoto(imageUrl: imageUrl);
|
||
}
|
||
}
|
||
|
||
static Future<dynamic> imageRequest({
|
||
required String imageUrl,
|
||
bool? isHideLoading,
|
||
}) async {
|
||
if (isHideLoading == true) {
|
||
} else {
|
||
await EasyLoading.show(
|
||
// status: 'loading...',
|
||
maskType: EasyLoadingMaskType.black,
|
||
);
|
||
}
|
||
|
||
var response = await Dio().get(
|
||
imageUrl,
|
||
options: Options(
|
||
responseType: ResponseType.bytes,
|
||
),
|
||
);
|
||
if (isHideLoading == true) {
|
||
} else {
|
||
EasyLoading.dismiss();
|
||
}
|
||
|
||
final result = await ImageGallerySaverPlus.saveImage(
|
||
Uint8List.fromList(response.data),
|
||
quality: 60,
|
||
name: "flutter_common_${DateTime.now().millisecondsSinceEpoch}",
|
||
isReturnImagePathOfIOS: true,
|
||
);
|
||
return result;
|
||
}
|
||
|
||
///fetchImageAsUint8List
|
||
static Future<Uint8List?> fetchImageAsUint8List(String imageUrl) async {
|
||
await EasyLoading.show(
|
||
// status: 'loading...',
|
||
maskType: EasyLoadingMaskType.black,
|
||
);
|
||
try {
|
||
var response = await Dio().get(
|
||
imageUrl,
|
||
options: Options(
|
||
responseType: ResponseType.bytes,
|
||
),
|
||
);
|
||
// final response = await http.get(Uri.parse(imageUrl));
|
||
if (response.statusCode == 200) {
|
||
EasyLoading.dismiss();
|
||
return response.data;
|
||
} else {
|
||
// debugPrint('Failed to load image: ${response.statusCode}');
|
||
EasyLoading.dismiss();
|
||
return null;
|
||
}
|
||
} catch (e) {
|
||
EasyLoading.dismiss();
|
||
// debugPrint('Error fetching image: $e');
|
||
return null;
|
||
}
|
||
}
|
||
}
|