3

Quite new to Flutter so this question might sound obvious but hey… I am trying to use flutter_email_sender (https://pub.dartlang.org/packages/flutter_email_sender) package to attach a .csv file to an email. Here is my code:

    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;
    }

And the stack trace:

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)

Note that "/data" appears twice in the path in the error message shown above. I have also tried to set the path manually to remove the additional one, to no avail.

Thank you in advance for your help :)

2 Answers 2

12

In:

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

You should instead use:

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

My understanding is as follow, you can read and write in ApplicationDocumentsDirectory but the data can't be accessed outside of the app. Since when you call FlutterEmailSender the data is then sent through an other application, you'll have to save your .csv to a "public" location. If you really want the data to be in the application documents directory, I encourage to investigate further on https://developer.android.com/reference/androidx/core/content/FileProvider But you'll probably have to write a little of platform specific code.

Regards

3
  • 1
    in case of using getExternalStorageDirectory,app will crash on iOS devices
    – Raj Kumar
    Dec 10, 2020 at 9:59
  • Is there any solution to it ? @RajKumar Feb 3, 2021 at 6:28
  • @TeekamSuthar Use following code: String dir = (await getApplicationDocumentsDirectory()).path; filePath = '$dir/$filename'; File file = new File('$dir/$filename');
    – Raj Kumar
    Feb 14, 2021 at 8:23
4

I don't know if you already got a solution, but anyway I'll leave my solution to help other Devs!

First, in /android/app/src/main/AndroidManifest.xml, configure the following:

(...)
</activity> 
   
<provider
     android:name="androidx.core.content.FileProvider"
     android:authorities="${applicationId}.com.shekarmudaliyar.social_share"
     android:exported="false"
     android:grantUriPermissions="true"
     tools:replace="android:authorities">
       <meta-data
         android:name="android.support.FILE_PROVIDER_PATHS"
         android:resource="@xml/filepaths" />
</provider>
(...)
<application>

-Then, create the filepaths.xml file inside a folder called xml, which must be created in the following path: /android/app/src/main/res/xml, containing the following:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <root-path name="root" path="." />
  <external-path name="external" path="." />
  <external-files-path name="external_files" path="." />
  <cache-path name="cache" path="." />
  <external-cache-path name="external_cache" path="." />
  <files-path name="files" path="." />
</paths>

That should be enough to stop the errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.