gpt4 book ai didi

file - flutter 发送邮件附件

转载 作者:IT王子 更新时间:2023-10-29 07:06:05 26 4
gpt4 key购买 nike

我的 flutter 应用程序使用 flutter_mailer: ^0.1.0 发送电子邮件。它在没有附件的情况下运行良好。但是如果我添加 attachments: [ x.path ],

  • 对于 IOS,当我按下按钮时,应用程序将立即退出。
  • 对于 Android,当我按下按钮时,应用程序不会打开电子邮件应用程序。请帮助解决这个问题。

这里是 Android 控制台的打印输出:

I/flutter ( 8933): /data/user/0/com.gph.testmanual/app_flutter/data.txt
I/fl ( 8933): [/data/user/0/com.gph.testmanual/app_flutter/data.txt]
E/MethodChannel#flutter_mailer( 8933): Failed to handle method call
E/MethodChannel#flutter_mailer( 8933): java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.gph.testmanual/app_flutter/data.txt

这里是 IOS 控制台的打印输出:

Launching lib/main.dart on iPhone in debug mode...
Starting Xcode build...
Xcode build done.
Installing and launching...
Syncing files to device iPhone...
flutter: /var/mobile/Containers/Data/Application/D87ECFEF-A989-345D-AE543C-1CE5AF96546345F52/Documents/data.txt

我的代码:

class _HomeState extends State<Home> {
File x;

@override
void initState() {
// TODO: implement initState
super.initState();

writeData('Hello, How are you?').then((value) {
x = value;
});
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Read/Write'),
centerTitle: true,
backgroundColor: Colors.greenAccent,
),
body: new Container(
padding: const EdgeInsets.all(13.4),
alignment: Alignment.topCenter,
child:
FlatButton(onPressed: () => _sendEmail(), child: Text("Button"))),
);
}

void _sendEmail() async {
debugPrint(x.path);
final MailOptions mailOptions = MailOptions(
body: 'a long body for the email',
subject: 'Booking',
recipients: ['gph.payment@gmail.com'],
isHTML: false,
// bccRecipients: ['other@example.com'],
// ccRecipients: ['third@example.com'],
// attachments: [ x.path ],
);

await FlutterMailer.send(mailOptions);
}
}

Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path; //home/directory/
}

Future<File> get _localFile async {
final path = await _localPath;

return new File('$path/data.txt'); //home/directory/data.txt
}

//Write and Read from our file
Future<File> writeData(String message) async {
final file = await _localFile;

//write to file
return file.writeAsString('$message');
}

Future<String> readData() async {
try {
final file = await _localFile;

//Read
String data = await file.readAsString();

return data;
} catch (e) {
return "Nothing saved yet!";
}
}

最佳答案

问题似乎出在应用程序文档目录上——出于某种原因,扩展程序还不能从中共享。 (刚刚报告了一个 bug for that 。)

我的解决方法是将文件复制到临时目录下并从那里附加它。

关于file - flutter 发送邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52548823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com