How to share a file using flutter

前端 未结 7 1291
悲哀的现实
悲哀的现实 2021-02-07 09:43

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 sta

相关标签:
7条回答
  • 2021-02-07 10:05

    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).

    0 讨论(0)
  • 2021-02-07 10:10

    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.

    0 讨论(0)
  • 2021-02-07 10:16

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

    }

    0 讨论(0)
  • 2021-02-07 10:18

    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.

    0 讨论(0)
  • 2021-02-07 10:19

    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

    0 讨论(0)
  • 2021-02-07 10:21

    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.

    0 讨论(0)
提交回复
热议问题