Skip to content

Commit

Permalink
fix: 安卓图片存储权限调整,仅在Android13及以上使用相册授权
Browse files Browse the repository at this point in the history
  • Loading branch information
orz12 committed Apr 22, 2024
1 parent 5d59865 commit ce45970
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions lib/utils/download.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:typed_data';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:saver_gallery/saver_gallery.dart';
import 'dart:io';

class DownloadUtils {
// 获取存储权限
Expand Down Expand Up @@ -43,39 +45,52 @@ class DownloadUtils {
PermissionStatus status = await Permission.photos.status;
if (status == PermissionStatus.denied ||
status == PermissionStatus.permanentlyDenied) {
SmartDialog.show(
useSystem: true,
animationType: SmartAnimationType.centerFade_otherSlide,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('提示'),
content: const Text('相册权限未授权'),
actions: [
TextButton(
onPressed: () async {
openAppSettings();
},
child: const Text('去授权'),
)
],
);
},
);
// SmartDialog.show(
// useSystem: true,
// animationType: SmartAnimationType.centerFade_otherSlide,
// builder: (BuildContext context) {
// return AlertDialog(
// title: const Text('提示'),
// content: const Text('相册权限未授权'),
// actions: [
// TextButton(
// onPressed: () async {
// openAppSettings();
// },
// child: const Text('去授权'),
// )
// ],
// );
// },
// );
return false;
} else {
return true;
}
}

static Future<bool> checkPermissionDependOnSdkInt() async {
if (Platform.isAndroid) {
final androidInfo = await DeviceInfoPlugin().androidInfo;
if (androidInfo.version.sdkInt <= 32) {
return await requestStoragePer();
} else {
return await requestPhotoPer();
}
}
return await requestStoragePer();
}
static Future<bool> downloadImg(String imgUrl,
{String imgType = 'cover'}) async {
try {
if (!await requestPhotoPer()) {
return false;
if (!await checkPermissionDependOnSdkInt()) {
// // return false;
}
SmartDialog.showLoading(msg: '保存中');
SmartDialog.showLoading(msg: '正在下载原图');
var response = await Dio()
.get(imgUrl, options: Options(responseType: ResponseType.bytes));
SmartDialog.dismiss();
SmartDialog.showLoading(msg: '正在保存图片至图库');
String picName =
"${imgType}_${DateTime.now().toString().replaceAll(' ', '_').replaceAll(':', '-').split('.').first}";
final SaveResult result = await SaverGallery.saveImage(
Expand Down

0 comments on commit ce45970

Please sign in to comment.