fix: 修复macos保存图片
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -15,35 +16,44 @@ class HIAntiLostController extends GetxController {
|
||||
Future<void> saveImage() async {
|
||||
// KRCommonUtil.kr_showLoading(message: "正在保存...");
|
||||
try {
|
||||
// Check permission
|
||||
final hasAccess = await Gal.hasAccess();
|
||||
if (!hasAccess) {
|
||||
await Gal.requestAccess();
|
||||
}
|
||||
|
||||
// Load image from assets
|
||||
final ByteData data =
|
||||
await rootBundle.load('assets/images/lost-share-image.png');
|
||||
final Uint8List pngBytes = data.buffer.asUint8List();
|
||||
|
||||
await Gal.putImageBytes(pngBytes);
|
||||
if (Platform.isMacOS || Platform.isWindows || Platform.isLinux) {
|
||||
// Desktop: Use FilePicker to save file
|
||||
String? outputFile = await FilePicker.platform.saveFile(
|
||||
dialogTitle: '保存图片',
|
||||
fileName: 'Hi快联系方式',
|
||||
type: FileType.custom,
|
||||
allowedExtensions: ['png'],
|
||||
);
|
||||
|
||||
String message = "已保存至系统相册";
|
||||
if (Platform.isMacOS) {
|
||||
message = "已保存至系统【照片】应用";
|
||||
} else if (Platform.isWindows) {
|
||||
message = "已保存至系统【图片】文件夹";
|
||||
if (outputFile != null) {
|
||||
final file = File(outputFile);
|
||||
await file.writeAsBytes(pngBytes);
|
||||
KRCommonUtil.kr_showToast("已保存至: $outputFile");
|
||||
}
|
||||
} else {
|
||||
// Mobile: Use Gal to save to gallery
|
||||
// Check permission
|
||||
final hasAccess = await Gal.hasAccess();
|
||||
if (!hasAccess) {
|
||||
await Gal.requestAccess();
|
||||
}
|
||||
|
||||
await Gal.putImageBytes(pngBytes);
|
||||
HIDialog.show(
|
||||
message: "已保存至系统相册",
|
||||
);
|
||||
}
|
||||
|
||||
HIDialog.show(
|
||||
message: message,
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint("Save image error: $e");
|
||||
if (e is GalException) {
|
||||
KRCommonUtil.kr_showToast("保存失败: No Permission");
|
||||
} else {
|
||||
KRCommonUtil.kr_showToast("保存失败");
|
||||
KRCommonUtil.kr_showToast("保存失败: $e");
|
||||
}
|
||||
} finally {
|
||||
//KRCommonUtil.kr_hideLoading();
|
||||
|
||||
Reference in New Issue
Block a user