mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
fix: 安卓图片存储权限调整,仅在Android13及以上使用相册授权
This commit is contained in:
@@ -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 {
|
||||
// 获取存储权限
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user