gpt4 book ai didi

android - Flutter:PlatformException(错误,无法找到包含 的已配置根目录,null)

转载 作者:行者123 更新时间:2023-12-03 04:38:27 29 4
gpt4 key购买 nike

对 Flutter 很陌生,所以这个问题听起来很明显,但是嘿……我正在尝试使用 flutter_email_sender ( https://pub.dartlang.org/packages/flutter_email_sender) 包将 .csv 文件附加到电子邮件中。这是我的代码:

    Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();

return directory.absolute.path;
}

Future<int> export(String csv) {
final String path = await _localPath + "/" + file_name;
File file = File(path);
file.writeAsString(csv);

File readFile = File(path);
print("content: ${await readFile.readAsString()}");
// the two lines above print the content of the file, as expected

final Email email = Email(
body: 'blah blah',
subject: 'blah blaaaah',
recipients: ["a@a.com"],
attachmentPaths: [file.path],
);

String platformResponse;

try {
print("Trying to send email.");
await FlutterEmailSender.send(email);
platformResponse = 'success';
} catch (error) {
print("Error");
print(error);
platformResponse = error.toString();
}

return platformResponse;
}

和堆栈跟踪:

E/MethodChannel#flutter_email_sender( 3433): Failed to handle method call
E/MethodChannel#flutter_email_sender( 3433): java.lang.IllegalArgumentException: Failed to find configured root that contains /data/package.name/app_flutter/fileName.csv
E/MethodChannel#flutter_email_sender( 3433): at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
E/MethodChannel#flutter_email_sender( 3433): at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
E/MethodChannel#flutter_email_sender( 3433): at com.sidlatau.flutteremailsender.FlutterEmailSenderPlugin.sendEmail(FlutterEmailSenderPlugin.kt:102)
E/MethodChannel#flutter_email_sender( 3433): at com.sidlatau.flutteremailsender.FlutterEmailSenderPlugin.onMethodCall(FlutterEmailSenderPlugin.kt:41)
E/MethodChannel#flutter_email_sender( 3433): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:230)
E/MethodChannel#flutter_email_sender( 3433): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#flutter_email_sender( 3433): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:683)
E/MethodChannel#flutter_email_sender( 3433): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#flutter_email_sender( 3433): at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#flutter_email_sender( 3433): at android.os.Looper.loop(Looper.java:160)
E/MethodChannel#flutter_email_sender( 3433): at android.app.ActivityThread.main(ActivityThread.java:6863)
E/MethodChannel#flutter_email_sender( 3433): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter_email_sender( 3433): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
E/MethodChannel#flutter_email_sender( 3433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/flutter ( 3433): Error
I/flutter ( 3433): PlatformException(error, Failed to find configured root that contains /data/data/package.name/app_flutter/fileName.csv, null)

请注意,“/data”在上面显示的错误消息中的路径中出现了两次。我也尝试手动设置路径以删除额外的路径,但无济于事。

预先感谢您的帮助:)

最佳答案

在:

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

你应该改用:

Future<String> get _localPath async {
final directory = await getExternalStorageDirectory();
return directory.absolute.path;
}

我的理解是这样的,你可以在ApplicationDocumentsDirectory中读写,但是数据不能在app外访问。由于当您调用 FlutterEmailSender 时,数据会通过其他应用程序发送,因此您必须将 .csv 文件保存到“公共(public)”位置。如果您真的希望数据位于应用程序文档目录中,我鼓励您进一步调查 https://developer.android.com/reference/androidx/core/content/FileProvider但是您可能必须编写一些特定于平台的代码。

问候

关于android - Flutter:PlatformException(错误,无法找到包含 <path> 的已配置根目录,null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63688285/

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