Skip to content

Commit

Permalink
Fix save to gallery on Android 11
Browse files Browse the repository at this point in the history
  • Loading branch information
ruskakimov committed Jun 15, 2021
1 parent ffc9bb9 commit 3980f3e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
38 changes: 17 additions & 21 deletions lib/editing/data/exporter_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)),
);
Expand All @@ -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<void> _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<void> openOutputFile() async {
if (_outputFilePath == null) return;
OpenFile.open(p.fromUri(_outputFilePath));
Expand Down
14 changes: 7 additions & 7 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3980f3e

Please sign in to comment.