Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share a file using flutter

Tags:

flutter

dart

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.

like image 562
David Roundy Avatar asked Dec 13 '16 19:12

David Roundy


2 Answers

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.

like image 146
Daniel Avatar answered Oct 21 '22 01:10

Daniel


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");

}

like image 44
FedeH Avatar answered Oct 21 '22 01:10

FedeH