解决方法:
1、引入permission_handler获取系统权限,引入image_gallery_saver存储图片
2、iOS与Android访问相册权限差异性,iOS获取photos权限,Android获取storage权限
// 图片下载
downLoadImage(
{BuildContext context, String source = '案例详情', String url}) async {
bool data;
if (Platform.isAndroid) {
data = await Permission.storage.request().isGranted;
} else {
data = await Permission.photos.request().isGranted;
}
debugPrint('权限:$data');
if (!data) {
// showDesignToast('请开启应用相册访问权限');
Navigator.of(context).maybePop();
showCustomDialog(
context: context,
title: '',
content: 'dHome需要存储权限才能下载,请点击开启',
buttons: ['暂不开启', '开启'],
onTap: (index) {
if (index == 0) {
Navigator.of(context).pop();
} else {
var hasOpened = openAppSettings();
debugPrint('App Settings opened: ' + hasOpened.toString());
}
});
return false;
}
/// 保存的图片数据
var response =
await Dio().get(url, options: Options(responseType: ResponseType.bytes));
final result = await ImageGallerySaver.saveImage(
Uint8List.fromList(response.data),
quality: 100,
);
debugPrint('下载$result');
Navigator.of(context).maybePop();
showDesignToast('下载成功');
}