如何使用flutter共享文件

问题描述 投票:9回答:6

我想知道如何在flutter应用程序中共享文件?

我看到了一些使用Intent和mojo的旧引用,但似乎不再存在。这似乎是我们应该能够以跨平台方式处理的标准功能(显然ios与android之间存在一些差异)。

目前共享文件的最佳做法是什么(例如通过电子邮件)?我能接受的最接近的是UrlLauncher,我可以想象它可以用来为我想要共享的文件启动一个处理程序,但它似乎是一个延伸。

dart flutter
6个回答
2
投票

对于仍然发现此问题的任何人,您现在可以与共享插件共享一些内容:https://pub.dartlang.org/packages/share

编辑:此插件仅支持文本/链接共享,这不足以直接共享文件。问题https://github.com/flutter/flutter/issues/16743https://github.com/flutter/flutter/issues/19607直接跟踪文件共享。


5
投票

目前没有内置的方法来做到这一点。正如Seth Ladd上面提到的那样,https://github.com/flutter/flutter/issues/7111正在追踪这一过程。

目前,您必须使用Objective-C或Java编写必要的共享代码,并使用https://flutter.io/platform-services中记录的https://github.com/flutter/flutter/tree/master/examples/hello_services中显示的平台服务模型从Dart调用它。


4
投票

你可以使用EsysFlutterShare Plugin。在1.0.0版中,您可以共享任何您喜欢的文件,并且可以在iOS和Android上共享它们。

把它放在你的pubspec.yaml中:

dependencies:
  esys_flutter_share: ^1.0.0

导入lib:

import 'package:esys_flutter_share/esys_flutter_share.dart';

分享文件:

final ByteData bytes = await rootBundle.load('assets/image1.png');
await Share.file('esys image', 'esys.png', bytes.buffer.asUint8List(), 'image/png');

您需要提供标题,文件名,文件字节和mime类型。


3
投票

对于任何想要反向分享的人,换句话说,让其他应用程序与您的扑动应用程序共享数据,请查看https://flutter.io/flutter-for-android/#how-do-i-handle-incoming-intents-from-external-applications-in-flutter

感谢GünterZöchbauer在飘动的Gitter频道!

这是我发现的最接近问题的“分享”某个应用程序的问题,对于这个特定的用例似乎没有SO问题,因此这个答案在这里


2
投票

我发布了这个回复,因为接受的答案实际上没有回答这个问题。

使用flutter-share from this github repo你可以通过添加它作为依赖项来共享文件,导入它,然后使用命名的构造函数文件实例化Share类:Share.file(path: <String>, mimeType: ShareType, title: , text: );其中mimeType,title和text是可选的,然后在其上调用share。

它完全适用于Android,IOS部分目前正在开发以匹配Android部分(目前只有plainText共享)。


0
投票

我使用share_extend与path_provider结合来共享音频文件,它完美地运行。

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

}

© www.soinside.com 2019 - 2024. All rights reserved.