From 02e463d3ecc118cc21c78ba94a3de3463f543761 Mon Sep 17 00:00:00 2001 From: Akira Date: Thu, 16 May 2024 09:26:15 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AE=E3=83=A3=E3=83=A9=E3=83=AA=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E5=86=99=E7=9C=9F=E4=BF=9D=E5=AD=98=E3=81=8C=E3=81=A7?= =?UTF-8?q?=E3=81=8D=E3=81=AA=E3=81=8F=E3=81=AA=E3=81=A3=E3=81=9F=E3=83=90?= =?UTF-8?q?=E3=82=B0=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../destination/destination_controller.dart | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/pages/destination/destination_controller.dart b/lib/pages/destination/destination_controller.dart index 3b177c8..59f3dff 100644 --- a/lib/pages/destination/destination_controller.dart +++ b/lib/pages/destination/destination_controller.dart @@ -9,6 +9,7 @@ import 'package:geolocator/geolocator.dart'; import 'package:get/get.dart'; import 'package:intl/intl.dart'; import 'package:latlong2/latlong.dart'; +import 'package:permission_handler/permission_handler.dart'; import 'package:rogapp/main.dart'; import 'package:rogapp/model/destination.dart'; import 'package:rogapp/model/gps_data.dart'; @@ -1071,6 +1072,49 @@ class DestinationController extends GetxController { } } + Future _saveImageToGallery(String imagePath) async { + final status = await Permission.storage.status; + if (!status.isGranted) { + final result = await Permission.storage.request(); + if (!result.isGranted) { + // ユーザーがストレージの権限を拒否した場合の処理 + showDialog( + context: Get.context!, + builder: (BuildContext context) { + return AlertDialog( + title: Text('ストレージの権限が必要です'), + content: Text( + '画像をギャラリーに保存するには、ストレージの権限が必要です。アプリの設定画面で権限を許可してください。'), + actions: [ + TextButton( + child: Text('キャンセル'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + TextButton( + child: Text('設定'), + onPressed: () { + Navigator.of(context).pop(); + openAppSettings(); // アプリの設定画面を開く + }, + ), + ], + ); + } + ); + return; + } + } + + try { + final result = await ImageGallerySaver.saveFile(imagePath); + print('Image saved to gallery: $result'); + } catch (e) { + print('Failed to save image to gallery: $e'); + } + } + // 買い物ポイントを作成する関数です。 指定された目的地に対して買い物ポイントの処理を行います。 // // 買い物ポイントの作成に失敗した場合のエラーハンドリングを追加することを検討してください。 @@ -1079,7 +1123,8 @@ class DestinationController extends GetxController { DatabaseHelper db = DatabaseHelper.instance; await db.updateBuyPoint(destination, imageurl); populateDestinations(); - await _saveImageFromPath(imageurl); + //await _saveImageFromPath(imageurl); + await _saveImageToGallery(imageurl); if (indexController.currentUser.isNotEmpty) { double cpNum = destination.cp!; @@ -1135,7 +1180,10 @@ class DestinationController extends GetxController { imageurl = photos[0].path; } debugPrint("imageurl = ${imageurl}"); - await _saveImageFromPath(imageurl!); + //await _saveImageFromPath(imageurl!); + } + if (imageurl.isNotEmpty) { + await _saveImageToGallery(imageurl); } populateDestinations();