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.
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.
I used share_extend combine 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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With