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

标签 android flutter dart

对 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/

相关文章:

java - LibGDX 加速度计 - Android

android - Flutter Launcher 图标没有改变?

azure - 使用 azure devops 进行 flutter 构建管道

来自 dartlang 的 dart.io 示例在 dartpad 上抛出 'uncaught' 错误(初学者级别)

AndroidX 不兼容导致 Flutter Gradle 失败

css - 如何以编程方式更改属性的值?

android - 检查 Activity 是否处于 Activity 状态

android - 如何获取应用程序id(包+模块名)delphi XE5/XE6/XE7

android - 某些文件处理失败,请参阅日志了解详细信息

flutter - 如何在抖动中绕过Artboard(Rive)