fix: 安卓图片存储权限调整,仅在Android13及以上使用相册授权

This commit is contained in:
orz12
2024-04-22 20:21:17 +08:00
parent 5d598657eb
commit ce459701aa

View File

@@ -1,10 +1,12 @@
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:saver_gallery/saver_gallery.dart'; import 'package:saver_gallery/saver_gallery.dart';
import 'dart:io';
class DownloadUtils { class DownloadUtils {
// 获取存储权限 // 获取存储权限
@@ -43,39 +45,52 @@ class DownloadUtils {
PermissionStatus status = await Permission.photos.status; PermissionStatus status = await Permission.photos.status;
if (status == PermissionStatus.denied || if (status == PermissionStatus.denied ||
status == PermissionStatus.permanentlyDenied) { status == PermissionStatus.permanentlyDenied) {
SmartDialog.show( // SmartDialog.show(
useSystem: true, // useSystem: true,
animationType: SmartAnimationType.centerFade_otherSlide, // animationType: SmartAnimationType.centerFade_otherSlide,
builder: (BuildContext context) { // builder: (BuildContext context) {
return AlertDialog( // return AlertDialog(
title: const Text('提示'), // title: const Text('提示'),
content: const Text('相册权限未授权'), // content: const Text('相册权限未授权'),
actions: [ // actions: [
TextButton( // TextButton(
onPressed: () async { // onPressed: () async {
openAppSettings(); // openAppSettings();
}, // },
child: const Text('去授权'), // child: const Text('去授权'),
) // )
], // ],
); // );
}, // },
); // );
return false; return false;
} else { } else {
return true; 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, static Future<bool> downloadImg(String imgUrl,
{String imgType = 'cover'}) async { {String imgType = 'cover'}) async {
try { try {
if (!await requestPhotoPer()) { if (!await checkPermissionDependOnSdkInt()) {
return false; // // return false;
} }
SmartDialog.showLoading(msg: '保存中'); SmartDialog.showLoading(msg: '正在下载原图');
var response = await Dio() var response = await Dio()
.get(imgUrl, options: Options(responseType: ResponseType.bytes)); .get(imgUrl, options: Options(responseType: ResponseType.bytes));
SmartDialog.dismiss();
SmartDialog.showLoading(msg: '正在保存图片至图库');
String picName = String picName =
"${imgType}_${DateTime.now().toString().replaceAll(' ', '_').replaceAll(':', '-').split('.').first}"; "${imgType}_${DateTime.now().toString().replaceAll(' ', '_').replaceAll(':', '-').split('.').first}";
final SaveResult result = await SaverGallery.saveImage( final SaveResult result = await SaverGallery.saveImage(