Fix crash on attempting to share multiple large files

This commit is contained in:
Vishnu Mohandas 2020-08-25 05:29:57 +05:30
parent 2458d5f35c
commit 0c70cd96fd
4 changed files with 48 additions and 13 deletions

View file

@ -60,6 +60,14 @@ void preloadLocalFileThumbnail(File file) {
});
}
Future<io.File> getNativeFile(File file) async {
if (file.localID == null) {
return getFileFromServer(file);
} else {
return file.getAsset().then((asset) => asset.file);
}
}
Future<Uint8List> getBytes(File file, {int quality = 100}) async {
if (file.localID == null) {
return getFileFromServer(file).then((file) => file.readAsBytesSync());

View file

@ -1,14 +1,44 @@
import 'dart:typed_data';
import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'package:flutter/widgets.dart';
import 'package:photos/models/file.dart';
import 'package:path/path.dart';
import 'package:photos/models/file_type.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/file_util.dart';
import 'package:progress_dialog/progress_dialog.dart';
import 'package:share_extend/share_extend.dart';
Future<void> share(BuildContext context, File file) async {
final dialog = createProgressDialog(context, "Preparing...");
if (file.fileType == FileType.image) {
return _shareImage(dialog, file);
} else {
return _shareVideo(dialog, file);
}
}
Future<void> shareMultiple(BuildContext context, List<File> files) async {
if (files.length == 1) {
return share(context, files[0]);
}
final dialog = createProgressDialog(context, "Preparing...");
await dialog.show();
final pathList = List<String>();
for (File file in files) {
pathList.add((await getNativeFile(file)).path);
}
await dialog.hide();
return ShareExtend.shareMultiple(pathList, "image");
}
Future<void> _shareVideo(ProgressDialog dialog, File file) async {
await dialog.show();
final path = (await getNativeFile(file)).path;
await dialog.hide();
return ShareExtend.share(path, "image");
}
Future<void> _shareImage(ProgressDialog dialog, File file) async {
await dialog.show();
final bytes = await getBytes(file);
final filename = _getFilename(file.title);
@ -20,17 +50,6 @@ Future<void> share(BuildContext context, File file) async {
return Share.file(filename, filename, bytes, "image/" + shareExt);
}
Future<void> shareMultiple(BuildContext context, List<File> files) async {
final dialog = createProgressDialog(context, "Preparing...");
await dialog.show();
final shareContent = Map<String, Uint8List>();
for (File file in files) {
shareContent[_getFilename(file.title)] = await getBytes(file);
}
await dialog.hide();
return Share.files("images", shareContent, "*/*");
}
String _getFilename(String name) {
if (name.endsWith(".HEIC")) {
return name.substring(0, name.lastIndexOf(".HEIC")) + ".JPG";

View file

@ -541,6 +541,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0"
share_extend:
dependency: "direct main"
description:
name: share_extend
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.9"
shared_preferences:
dependency: "direct main"
description:

View file

@ -35,6 +35,7 @@ dependencies:
crypto: ^2.1.3
image: ^2.1.4
esys_flutter_share: ^1.0.2
share_extend: ^1.1.9
draggable_scrollbar: ^0.0.4
photo_view: ^0.9.2
visibility_detector: ^0.1.5