Xam.Plugins.Messaging, Sending an attachment

JungleJungle Member ✭✭
edited October 2018 in Xamarin.Forms

I am banging my head against a brick wall with this.

I'm using XF 3.2, XAM plugin 5.2, sending an email with out an attachment is fine.

I have the following code;

        var emailSender = CrossMessaging.Current.EmailMessenger;

        var email = new EmailMessageBuilder()
            .Subject("xyz")
            .Body("redacted")
            .WithAttachment()
            .Build();

        emailSender.SendEmail(email);

Now regardless what I try, local PCL path(string, string) , "content//", i can not get anything other than this Error, when sending the email with attachment.

Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference

when I watch the "email", the Java.io.file is null still.

I think I need to use a dependency service to send the email, and then i could use the native file formats, but i would still need to know the full correct path to lets say assets or resources on both platforms.

It doesn't help that the documentation seems vastly outdated, poorly structured, and assumes a lot of prior knowledge.

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    @Jungle
    Have you followed the guidance in the README.md for setting up the FileProvider in your AndroidManifest.xml?

  • JungleJungle Member ✭✭

    @jezh said:
    @Jungle
    Have you followed the guidance in the README.md for setting up the FileProvider in your AndroidManifest.xml?

    Yeah, i have added the code given to the tag, and then created the xml folder and the xml document folder_path, i deleted it all and redid it just to make sure.

    the "withattachment" needs a java.io.file, which takes a java.net.URI, but i dont know, and cant find any references, to the path it needs to lets say the asset folder in "appName.Android"

  • jezhjezh Member, Xamarin Team Xamurai

    Could you share a basic demo so that we can test it?

  • JohnSmith.0620JohnSmith.0620 USMember ✭✭

    I ran into this issue just now and managed to fix it. For me, I had my authorities property set to my namespace. This should have been the package name (ex: com.company.myapp):

    android:authorities="com.company.myapp.fileprovider" 
    
  • I'm having a similar problems, maybe I don't get something, but I did follow the readme instruction and added the AndroidManifest.xml entry:
    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="LINK_REMOVED TO POST" android:versionCode="1" android:versionName="1.0" package="com.companyname.GF_Trace" android:installLocation="auto"> <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="27" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:label="GF_Trace.Android"> <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.companyname.GF_Trace.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data> </provider> </application> </manifest>

    Into the Resources, I created an xml folder that content file_pathd.xml Here I did try a few things to try to get it working:
    <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="LINK REMOVED TO POST"> <external-files-path name="exttempopdf" path="tempo.pdf" /> <external-path name="extpdfs" path="." /> <cache-path name="cachepdf" path="." /> <external-cache-path name="extcachepdf" path="." /> </paths>

    But my attachment I provide the file path:
    /data/user/0/com.companyname.GF_Trace/files/.local/share/tempo.pdf
    That I create using:
    string folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); string file = @"tempo.pdf"; string path = System.IO.Path.Combine(folder, file);
    But the exception raise when sending the email is:
    Java.Lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.companyname.GF_Trace/files/.local/share/tempo.pdf occurred

    So it seem to miss something, I inspected the path, into the email builder and it still the /data/user/0/com.companyname.GF_Trace/files/.local/share/tempo.pdf
    But I guess the /data/user/0 is a symlink to /data/data and this should work.
    Any help or working example would be welcome.

Sign In or Register to comment.