Skip to content

An open-source cross-platform alternative to AirDrop

feat: add save to gallery

Tien Do Nam5b49ef5c7f5b
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 560e55a9..a524ae67 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -7,7 +7,8 @@
android:label="LocalSend"
android:name="${applicationName}"
android:usesCleartextTraffic="true"
- android:icon="@mipmap/ic_launcher">
+ android:icon="@mipmap/ic_launcher"
+ android:requestLegacyExternalStorage="true"> <!-- image_gallery_saver requires requestLegacyExternalStorage -->
<activity
android:name=".MainActivity"
android:exported="true"
diff --git a/ios/Podfile.lock b/ios/Podfile.lock
index dd1a639e..76bfe7ed 100644
--- a/ios/Podfile.lock
+++ b/ios/Podfile.lock
@@ -36,6 +36,8 @@ PODS:
- DKImagePickerController/PhotoGallery
- Flutter
- Flutter (1.0.0)
+ - image_gallery_saver (1.5.0):
+ - Flutter
- image_picker_ios (0.0.1):
- Flutter
- network_info_plus (0.0.1):
@@ -50,16 +52,20 @@ PODS:
- shared_preferences_ios (0.0.1):
- Flutter
- SwiftyGif (5.4.3)
+ - url_launcher_ios (0.0.1):
+ - Flutter
DEPENDENCIES:
- device_info_plus (from `.symlinks/plugins/device_info_plus/ios`)
- file_picker (from `.symlinks/plugins/file_picker/ios`)
- Flutter (from `Flutter`)
+ - image_gallery_saver (from `.symlinks/plugins/image_gallery_saver/ios`)
- image_picker_ios (from `.symlinks/plugins/image_picker_ios/ios`)
- network_info_plus (from `.symlinks/plugins/network_info_plus/ios`)
- package_info_plus (from `.symlinks/plugins/package_info_plus/ios`)
- path_provider_ios (from `.symlinks/plugins/path_provider_ios/ios`)
- shared_preferences_ios (from `.symlinks/plugins/shared_preferences_ios/ios`)
+ - url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
SPEC REPOS:
trunk:
@@ -75,6 +81,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/file_picker/ios"
Flutter:
:path: Flutter
+ image_gallery_saver:
+ :path: ".symlinks/plugins/image_gallery_saver/ios"
image_picker_ios:
:path: ".symlinks/plugins/image_picker_ios/ios"
network_info_plus:
@@ -85,6 +93,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/path_provider_ios/ios"
shared_preferences_ios:
:path: ".symlinks/plugins/shared_preferences_ios/ios"
+ url_launcher_ios:
+ :path: ".symlinks/plugins/url_launcher_ios/ios"
SPEC CHECKSUMS:
device_info_plus: e5c5da33f982a436e103237c0c85f9031142abed
@@ -92,6 +102,7 @@ SPEC CHECKSUMS:
DKPhotoGallery: fdfad5125a9fdda9cc57df834d49df790dbb4179
file_picker: 817ab1d8cd2da9d2da412a417162deee3500fc95
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
+ image_gallery_saver: 259eab68fb271cfd57d599904f7acdc7832e7ef2
image_picker_ios: b786a5dcf033a8336a657191401bfdf12017dabb
network_info_plus: b78876159360f5580608c2cea620d6ceffabd0ad
package_info_plus: 6c92f08e1f853dc01228d6f553146438dafcd14e
@@ -99,6 +110,7 @@ SPEC CHECKSUMS:
SDWebImage: b9a731e1d6307f44ca703b3976d18c24ca561e84
shared_preferences_ios: 548a61f8053b9b8a49ac19c1ffbc8b92c50d68ad
SwiftyGif: 6c3eafd0ce693cad58bb63d2b2fb9bacb8552780
+ url_launcher_ios: 839c58cdb4279282219f5e248c3321761ff3c4de
PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3
diff --git a/ios/Runner/Info.plist b/ios/Runner/Info.plist
index 0999e423..bde0bfd1 100644
--- a/ios/Runner/Info.plist
+++ b/ios/Runner/Info.plist
@@ -58,5 +58,7 @@
<string>This app requires access to the camera.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app requires access to the microphone.</string>
+ <key>NSPhotoLibraryAddUsageDescription</key>
+ <string>This app requires photo library permission to save received media.</string>
</dict>
</plist>
diff --git a/lib/provider/network/server_provider.dart b/lib/provider/network/server_provider.dart
index 50ff7942..19ea44c3 100644
--- a/lib/provider/network/server_provider.dart
+++ b/lib/provider/network/server_provider.dart
@@ -7,6 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:localsend_app/model/dto/info_dto.dart';
import 'package:localsend_app/model/dto/send_request_dto.dart';
import 'package:localsend_app/model/file_status.dart';
+import 'package:localsend_app/model/file_type.dart';
import 'package:localsend_app/model/server/receive_state.dart';
import 'package:localsend_app/model/server/receiving_file.dart';
import 'package:localsend_app/model/server/server_state.dart';
@@ -195,6 +196,8 @@ class ServerNotifier extends StateNotifier<ServerState?> {
try {
await saveFile(
destinationPath: destinationPath,
+ saveToGallery: (defaultTargetPlatform == TargetPlatform.android || defaultTargetPlatform == TargetPlatform.iOS) &&
+ (receivingFile.file.fileType == FileType.image || receivingFile.file.fileType == FileType.video),
stream: request.read(),
onProgress: (savedBytes) {
if (receivingFile.file.size != 0) {
diff --git a/lib/util/file_saver.dart b/lib/util/file_saver.dart
index 5f60b3a6..10e8c000 100644
--- a/lib/util/file_saver.dart
+++ b/lib/util/file_saver.dart
@@ -1,9 +1,12 @@
import 'dart:io';
+import 'package:image_gallery_saver/image_gallery_saver.dart';
+
/// Saves the data [stream] to the [destinationPath].
/// [onProgress] will be called on every 100 KB.
Future<void> saveFile({
required String destinationPath,
+ required bool saveToGallery,
required Stream<List<int>> stream,
required void Function(int savedBytes) onProgress,
}) async {
@@ -21,6 +24,13 @@ Future<void> saveFile({
}
});
+ await sink.close();
+
+ if (saveToGallery) {
+ await ImageGallerySaver.saveFile(destinationPath);
+ await File(destinationPath).delete();
+ }
+
onProgress(savedBytes); // always emit final event
} catch (_) {
try {
@@ -30,11 +40,5 @@ Future<void> saveFile({
print(e);
}
rethrow;
- } finally {
- try {
- await sink.close();
- } catch (e) {
- print(e);
- }
}
}
diff --git a/pubspec.lock b/pubspec.lock
index 1abdb496..47f42b5d 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -340,6 +340,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.2"
+ image_gallery_saver:
+ dependency: "direct main"
+ description:
+ name: image_gallery_saver
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "1.7.1"
image_picker:
dependency: "direct main"
description:
diff --git a/pubspec.yaml b/pubspec.yaml
index 42b7d9c8..1fb06e9b 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -18,6 +18,7 @@ dependencies:
flutter_markdown: 0.6.13
flutter_riverpod: 2.1.1
freezed_annotation: 2.2.0
+ image_gallery_saver: 1.7.1
image_picker: 0.8.6
json_annotation: 4.7.0
network_info_plus: 3.0.1