'How to share a file using flutter
I am wondering how to share a file in a flutter app?
I saw some old references to using Intents with mojo, but that no longer seems to be present. This seems like a standard feature that we should be able to handle in a cross-platform way (obviously with some differences between ios to android).
What is the current best practices for sharing files (e.g. via email)? The closest thing I could fine is UrlLauncher, which I could imagine using to launch a handler for a file that I want shared, but it seems a stretch.
Solution 1:[1]
For anybody still finding this question, you can now share something with the share plugin: https://pub.dartlang.org/packages/share
Edit: This plugin only supports text/link share which is not sufficient for directly sharing files. The issues https://github.com/flutter/flutter/issues/16743 and https://github.com/flutter/flutter/issues/19607 track file sharing directly in flutter.
Solution 2:[2]
You can use the EsysFlutterShare Plugin. In version 1.0.0 you can share any file you like and its working on both, iOS and Android.
Just put that in yout pubspec.yaml:
dependencies:
esys_flutter_share: ^1.0.0
Import the lib:
import 'package:esys_flutter_share/esys_flutter_share.dart';
Share a file:
final ByteData bytes = await rootBundle.load('assets/image1.png');
await Share.file('esys image', 'esys.png', bytes.buffer.asUint8List(), 'image/png');
You need to privide a title, a file name, the file-bytes and a mime type.
Solution 3:[3]
I used share_extend combined with path_provider to share an audio file and it works perfectly.
void share() async {
Directory dir = await getApplicationDocumentsDirectory();
File testFile = new File("${dir.path}/sound.m4a");
if (!await testFile.exists()) {
await testFile.create(recursive: true);
testFile.writeAsStringSync("test for share documents file");
}
ShareExtend.share(testFile.path, "file");
}
Solution 4:[4]
For anyone looking to reverse share or in other words, have other apps share data to your flutter app, have a look at https://flutter.io/flutter-for-android/#how-do-i-handle-incoming-intents-from-external-applications-in-flutter .
Thanks to Günter Zöchbauer on the flutter Gitter channel!
This was the closest question I found to 'sharing' something to a flutter app and there doesn't seem to be a SO question for this particular usecase, hence this answer here
Solution 5:[5]
Flutter just accepted a long time PR, the Share plugin can now also share files!
ex:
Share.shareFiles(['${directory.path}/image.jpg'], text: 'Great picture');
Share.shareFiles(['${directory.path}/image1.jpg', '${directory.path}/image2.jpg']);
Solution 6:[6]
Currently there is no built-in way to do this. As Seth Ladd mentioned above, https://github.com/flutter/flutter/issues/7111 is tracking making this easier.
For now, you would have to write the necessary share code in Objective-C or Java and call it from your Dart using the platform-services model documented at https://flutter.io/platform-services and shown in https://github.com/flutter/flutter/tree/master/examples/hello_services.
Solution 7:[7]
I'm posting this reply, since the accepted answer doesn't actually answer the question.
using flutter-share from this github repo you can share files by just adding it as dependency, import it, and then instantiate the Share class with the named constructor file like so: Share.file(path: <String>, mimeType: ShareType, title: , text: );
where mimeType, title and text are optional, and then call share on it.
It is fully functional for Android, the IOS part is currently being developed to match the Android part (only having plainText share at the moment).
Solution 8:[8]
Now, the best way is to use share_plus package
List<String> paths = [filePath];
await Share.shareFiles(
paths,
subject: 'subj',
text: 'text',
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | kalucki23 |
Solution 4 | gabbar0x |
Solution 5 | |
Solution 6 | Eric Seidel |
Solution 7 | |
Solution 8 | Nikita Shadkov |