From 3980f3e449fc3ba9183486954275b195378bc805 Mon Sep 17 00:00:00 2001 From: Rustem Kakimov Date: Tue, 15 Jun 2021 19:33:06 +0600 Subject: [PATCH] Fix save to gallery on Android 11 --- lib/editing/data/exporter_model.dart | 38 +++++++++++++--------------- pubspec.lock | 14 +++++----- pubspec.yaml | 4 +-- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/editing/data/exporter_model.dart b/lib/editing/data/exporter_model.dart index 88eab5f8..5210f263 100644 --- a/lib/editing/data/exporter_model.dart +++ b/lib/editing/data/exporter_model.dart @@ -2,7 +2,7 @@ import 'dart:io'; import 'package:firebase_crashlytics/firebase_crashlytics.dart'; import 'package:flutter/material.dart'; -import 'package:image_gallery_saver/image_gallery_saver.dart'; +import 'package:gallery_saver/gallery_saver.dart'; import 'package:mooltik/common/data/io/generate_image.dart'; import 'package:mooltik/common/data/io/mp4/mp4.dart'; import 'package:mooltik/common/data/project/composite_frame.dart'; @@ -52,7 +52,8 @@ class ExporterModel extends ChangeNotifier { _state = ExporterState.exporting; notifyListeners(); - final videoFile = _tempFile('mooltik_video.mp4'); + final timestamp = DateTime.now().millisecondsSinceEpoch; + final videoFile = _tempFile('mooltik_video_$timestamp.mp4'); final slides = await Future.wait( frames.map((frame) => _slideFromFrame(frame)), ); @@ -68,31 +69,26 @@ class ExporterModel extends ChangeNotifier { }, ); - if (Platform.isAndroid) { - // Save to gallery on Android. - try { - final galleryResult = await ImageGallerySaver.saveFile(videoFile.path); - final galleryFilePath = galleryResult['filePath']; - _outputFilePath = galleryFilePath ?? videoFile.path; - - if (galleryFilePath == null) { - throw Exception( - 'Failed when tried uploading video to the gallery. $galleryResult'); - } - } catch (e, stack) { - FirebaseCrashlytics.instance.recordError(e, stack); - } - } else if (Platform.isIOS) { - // iOS is more restrictive here. Usually apps show share dialog. - // Default native player will have a share button. - _outputFilePath = videoFile.path; - } + await _saveToGallery(videoFile.path); + _outputFilePath = videoFile.path; _progress = 1; // in case ffmpeg statistics callback didn't finish on 100% _state = ExporterState.done; notifyListeners(); } + Future _saveToGallery(String path) async { + try { + final success = await GallerySaver.saveVideo(path); + + if (success == false) { + throw Exception('Failed when tried uploading video to the gallery.'); + } + } catch (e, stack) { + FirebaseCrashlytics.instance.recordError(e, stack); + } + } + Future openOutputFile() async { if (_outputFilePath == null) return; OpenFile.open(p.fromUri(_outputFilePath)); diff --git a/pubspec.lock b/pubspec.lock index 587290e1..75596de8 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -254,6 +254,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "9.0.0" + gallery_saver: + dependency: "direct main" + description: + name: gallery_saver + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" http: dependency: transitive description: @@ -275,13 +282,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.2" - image_gallery_saver: - dependency: "direct main" - description: - name: image_gallery_saver - url: "https://pub.dartlang.org" - source: hosted - version: "1.6.9" io: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 62e9c8ea..218060fd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 1.11.1 +version: 1.11.2 environment: sdk: '>=2.12.0 <3.0.0' @@ -56,7 +56,7 @@ dependencies: # Export video: flutter_ffmpeg: ^0.4.0 - image_gallery_saver: ^1.6.9 + gallery_saver: ^2.1.0 open_file: ^3.2.1 # Analytics: