【问题标题】:How can I share voice file(mpeg) which in app files to other app(like whatsapp) in flutter如何在颤动中将应用程序文件中的语音文件(mpeg)共享给其他应用程序(如whatsapp)
【发布时间】:2021-04-13 00:09:25
【问题描述】:

我尝试使用flutter share 2.0.1 插件来共享语音文件,但它不起作用(没有这样的文件)。你能解决这个问题吗?或者我怎样才能将语音文件分享到其他应用程序?这是我的代码和错误截图。


E/flutter (31567): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(assets/voices/pırt.mpeg (No such file or directory), null, null, null)
E/flutter (31567): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:597:7)
E/flutter (31567): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18)

IconButton(
              icon: Icon(Icons.send),
              color: Colors.black,
              iconSize: 35,
              onPressed: () {
                try{
                  Share.shareFiles(["assets/voices/pırt.mpeg"],text: "Share");
                }
                catch(ex){
                  print(ex);
                }
              },
            ),

【问题讨论】:

  • 错误应该作为文本复制粘贴到您的问题中,就像您的代码一样。不是图片。
  • 谢谢 :) 你知道我的问题解决方案吗?
  • 我不确定“谢谢”是对我之前评论的回应。在得到答案之前,您必须先解决我所说的问题。
  • “谢谢”我说错了。我不知道,你教我一些东西:)
  • 这通常意味着对新知识采取某种行动。我看到您现在在错误中进行了编辑。这正是预期的结果。

标签: flutter dart share sharefile


【解决方案1】:

如错误所述,assets/voices/pırt.mpeg 不是操作系统上文件的有效路径。这是一种资产,打包到您的应用程序中。如果要共享资产,需要先在设备上制作文件。

您需要添加 path_provider 依赖项:

dependencies:
  path_provider: ^2.0.1

然后,新建一个File,并将资产中的数据写入File

//Get directory and make a file object
final Directory dir = await getTemporaryDirectory();
final File file = File(dir.path + '/mpeg_data');

//Get data from assets
ByteData data = await rootBundle.load('assets/voices/pırt.mpeg');

//Write actual data
await file.writeAsBytes(data.buffer.asUint8List());

这一切都应该在您共享文件之前完成。然后当你共享文件时,使用你创建的文件对象的路径:

//Get directory and make a file object
final Directory dir = await getTemporaryDirectory();
final File file = File(dir.path);

//Get data from assets
ByteData data = await rootBundle.load('assets/voices/pırt.mpeg');

//Write actual data
await file.writeAsBytes(data.buffer.asUint8List());

try{
  Share.shareFiles([file.path],text: "Share");
}
catch(ex){
  print(ex);
}

【讨论】:

  • E/flutter (32083): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.example.replik/cache' (OS Error: Is a directory, errno = 21) E/flutter (32083): #0 _File.open.<anonymous closure> (dart:io/file_impl.dart:356:9) E/flutter (32083): #1 _rootRunUnary (dart:async/zone.dart:1362:47) E/flutter (32083): #2 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
  • 我尝试了您的解决方案。我认为这解决了我的问题,但出现了另一个问题。
  • @MuhammetKorkmaz 我忘了添加文件名。查看编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
  • 2022-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多