通过flutter_email_sender发送带有附件的电子邮件在Android上不起作用

时间:2019-09-12 16:33:55

标签: flutter flutter-dependencies

我正在尝试使用flutter_email_sender发送带有pdf附件的电子邮件,它在iOS上工作正常,但在Android上抛出Failed to find configured root错误。下面是代码。

Future<void> _downloadFile(String url, String filename) async {
    var request = await httpClient.getUrl(Uri.parse(url));
    var response = await request.close();
    var bytes = await consolidateHttpClientResponseBytes(response);
    String dir = (await getApplicationDocumentsDirectory()).path;
    File file = new File('$dir/$filename');
    await file.writeAsBytes(bytes);
    setState(() {
      _file = file;
    });
  }

 final Email email = Email(
    body: 'Email body',
    subject: 'Email subject',
    recipients: ['email@gmail.com'],
    attachmentPath: _file.path,
    );
 await FlutterEmailSender.send(email);

和堆栈跟踪:

E/MethodChannel#flutter_email_sender: Failed to handle method call
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.xxx.xx/app_flutter/account_opening.pdf at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
            at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
            at com.sidlatau.flutteremailsender.FlutterEmailSenderPlugin.sendEmail(FlutterEmailSenderPlugin.kt:95)
            at com.sidlatau.flutteremailsender.FlutterEmailSenderPlugin.onMethodCall(FlutterEmailSenderPlugin.kt:38)
            at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
            at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
            at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:643)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:326)
            at android.os.Looper.loop(Looper.java:160)
            at android.app.ActivityThread.main(ActivityThread.java:6669)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    2019-09-11 13:44:42.484 26003-26003/com.xxx.xx W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@98fb11f
    2019-09-11 13:44:42.505 26003-26003/com.xxx.xx D/AndroidRuntime: Shutting down VM
    2019-09-11 13:44:42.512 26003-26003/com.xxx.xx E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.xxx.xx, PID: 26003
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xx/com.kiwi.fluttercrashlytics.CrashActivity}: com.kiwi.fluttercrashlytics.FlutterException: PlatformException(error, Failed to find configured root that contains /data/data/com.xxx.xx/app_flutter/account_opening.pdf, null)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
            at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
            at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
            at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:6669)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
         Caused by: com.kiwi.fluttercrashlytics.FlutterException: PlatformException(error, Failed to find configured root that contains /data/data/com.xxx.xx/app_flutter/account_opening.pdf, null)
            at StandardMethodCodec.decodeEnvelope(package:flutter/src/services/message_codecs.dart:564)
            at MethodChannel.invokeMethod(package:flutter/src/services/platform_channel.dart:316)
            at FlutterEmailSender.send(package:flutter_email_sender/flutter_email_sender.dart:10)
            at _EmailWidgetState.build.<fn>(package:gsec/shared/widgets/manualPDFWidget/email_widget.dart:136)
            at OnboardingNextButtonWidget.build.<fn>(package:gsec/onboardingScreen/onboard_next_button_widget.dart:84)
            at GestureRecognizer.invokeCallback(package:flutter/src/gestures/recognizer.dart:182)
            at TapGestureRecognizer._checkUp(package:flutter/src/gestures/tap.dart:365)
            at TapGestureRecognizer.acceptGesture(package:flutter/src/gestures/tap.dart:312)
            at GestureArenaManager.sweep(package:flutter/src/gestures/arena.dart:156)
            at _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent(package:flutter/src/gestures/binding.dart:222)
            at _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent(package:flutter/src/gestures/binding.dart:198)
            at _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent(package:flutter/src/gestures/binding.dart:156)
            at _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue(package:flutter/src/gestures/binding.dart:102)
            at _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket(package:flutter/src/gestures/binding.dart:86)
            at ._rootRunUnary(dart:async/zone.dart:1136)
            at _CustomZone.runUnary(dart:async/zone.dart:1029)
            at _CustomZone.runUnaryGuarded(dart:async/zone.dart:931)
            at ._invoke1(dart:ui/hooks.dart:250)
            at ._dispatchPointerDataPacket(dart:ui/hooks.dart:159)

1 个答案:

答案 0 :(得分:1)

无法附加来自ApplicationDocumentsDirectory的文件,因为只能从您的应用访问此目录。您必须使用如ExternalStorageDirectory之类的目录才能发送该目录。如果这样做,请不要忘记在发布前为应用添加WRITE_EXTERNAL_STORAGE权限。

干杯。