添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Hello all,

I am creating an app that uses MMS to send a picture to a contact. I currently have mostly everything created. The part that I am having trouble on is creating the URI from a file.

What I have right now is that I am using the FileProvider class to generate the URI (using the function GetUriFromFile).

However, when I run the code, I get the exception:

Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference\n  at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <26521a5118b44c858c385715922b9d5d>

I am not sure what is going on. Looking for a solution for a few days but I haven't found anything. Does anyone have any suggestions?

Below is a copy of my code:

public void SendMMSMessage()
                Android.Telephony.SmsManager smsMessage = Android.Telephony.SmsManager.Default;
                IList<string> divideContents = smsMessage.DivideMessage("I auto sent this message to you! No typing here!!!!!");
                string filePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures) + "/TestPic.jpg";
                byte[] sendPDUData = GetMMSPDUData("1111111111", filePath, "Hey this image was sent from my phone using code! No typing here");
                if (sendPDUData != null)
                    SendMMSData(sendPDUData);
        public byte[] GetMMSPDUData(string DestinationNumber, string AudioFilePath, string smsMessage)
            byte[] pduData = null;
                SendReq sendReq = new SendReq();
                sendReq.AddTo(new EncodedStringValue(DestinationNumber));
                PduBody pduBody = new PduBody();
                // Add text message data to message
                PduPart txtPart = new PduPart();
                txtPart.SetData(Encoding.ASCII.GetBytes(smsMessage));
                txtPart.SetContentType(new EncodedStringValue("text/plan").GetTextString());
                txtPart.SetName(new EncodedStringValue("Message").GetTextString());
                pduBody.AddPart(txtPart);
                // Add image data 
                // TODO: Later, this will be audio file. But image file for testing
                PduPart imgPart = new PduPart();
                byte[] sampleImageData = System.IO.File.ReadAllBytes(AudioFilePath);
                imgPart.SetData(sampleImageData);
                imgPart.SetContentType(new EncodedStringValue("image/jpg").GetTextString());
                imgPart.SetFilename(new EncodedStringValue(System.IO.Path.GetFileName(AudioFilePath)).GetTextString());
                pduBody.AddPart(imgPart);
                // Now create body of MMS
                sendReq.Body = pduBody;
                // Finally, generate the byte array to send to the MMS provider
                PduComposer composer = new PduComposer(sendReq);
                pduData = composer.Make();
            catch(Exception ex)
                // TODO: Do something here
            return pduData;
        public bool SendMMSData(byte[] PDUData)
            Context CTX = Android.App.Application.Context;
            Android.Telephony.SmsManager sm = Android.Telephony.SmsManager.Default;
            Random rnd = new Random();
                string cacheFilePath = System.IO.Path.Combine(CTX.CacheDir.AbsolutePath, "send." + rnd.Next().ToString() + ".dat");
                System.IO.File.WriteAllBytes(cacheFilePath, PDUData);
                Java.IO.File testFile = new Java.IO.File(cacheFilePath);
                string authString = CTX.PackageName + ".fileprovider";
                if (System.IO.File.Exists(cacheFilePath))
                    Android.Net.Uri contentURI = AndroidX.Core.Content.FileProvider.GetUriForFile(CTX, CTX.PackageName + ".fileprovider", testFile);
                    PendingIntent pendingIntent = PendingIntent.GetBroadcast(CTX, 0, new Intent(CTX.PackageName + ".WAP_PUSH_DELIVER"), 0);
                    sm.SendMultimediaMessage(CTX, contentURI, null, null, null);
            catch(Exception ex)
                String exString = ex.ToString();
                return false;
            return true;

Note: The number I made up. But I can guarantee you that I placed a working phone number in the function.

Do I need to do something with the Android Manifest XML file like this stack overflow post:

https://stackoverflow.com/questions/46550472/fileprovider-geturiforfile-returns-nullpointerexception

So I tried adding this into my manifest and I am getting the following error:

<application>
        <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.companyname.JanineSafety2.fileprovider" android:grantUriPermissions="true" android:exported="false">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
        </provider>
    </application>

Error:

resource xml/provider_paths (aka com.companyname.janinesafety2:xml/provider_paths) not found
												

Hi, PhillipMobley-0553. What's the version of the Xamarin.Forms? Or what's the version of the targetFramework in the Android project? Since Xamarin.Forms 5.0, it requires to set the TargetFramework to Android 10 which will using AndroidX library by defalut. In this case, please use the function code in AndroidX library.

resource xml/provider_paths (aka com.companyname.janinesafety2:xml/provider_paths) not found

This error occurs because manifest couldn't find the file you indicated here: android:resource="@xml/file_path". Did you create the 'provider_paths.xml' in the Resource\xml folder?

Hello, I am currently running v5.0.0.2012

My current Target is set to Android 9 which is what my phone runs. Will I still be able to run the application if the target and the actual are different versions?

Do I need to add in the xml code? I using the function for sending a picture and not an XML file.

If I do need the xml file, what goes into the provider path?

Ok so I created an xml file and I added this to the provider paths:

<?xml version="1.0" encoding="utf-8" ?>
<paths>
  <external-path
    name="external_files"
    path="/data/user/0/com.companyname.janinesafety2/cache/"
</paths>

I used /data/user/0/com.companyname.janinesafety2/cache/ as the path because this is the folder that the cache file is stored in. But when I run the function,
I get the following error:

Java.Lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.companyname.janinesafety2.fileprovider
												

Ok so I created an xml file and I added this to the provider paths:

 <?xml version="1.0" encoding="utf-8" ?>
 <paths>
   <external-path
     name="external_files"
     path="/data/user/0/com.companyname.janinesafety2/cache/"
 </paths>

I used /data/user/0/com.companyname.janinesafety2/cache/ as the path because this is the folder that the cache file is stored in. But when I run the function,
I get the following error:

 Java.Lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.companyname.janinesafety2.fileprovider
												

@Phillip Mobley

Hi Phillip,

I create applications in Xamarin, one part is sending MMS. This is the only page I found that describes MMS sending in Xamarin. When I debug everything works
SendMultimediaMessage seems to work as expected but MMS message is never sent.

Did you manage to send the MMS in Xamarin?

Welcome to our Microsoft Q&A platform!

Java.Lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.companyname.janxxxx.fileprovider

It seems there is something wrong with the applicationId. According to the error info, the package name is com.companyname.janxxxx. But in the <application> tag of AndroidManifest.xaml, it's com.companyname.Janxxxx. The j is an uppercase letter, please change it to lowercase and test again.

   //the funtion code  
   Android.Net.Uri contentURI = Android.Support.V4.Content.FileProvider.GetUriForFile(CTX, CTX.PackageName + ".fileprovider", testFile);  
   //the application tag  
   <application>  
     <provider android:authorities="com.companyname.Janxxx.fileprovider" ...>  
     </provider>  
   </application>  

Best Regards,

Jarvan Zhang

If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

I am currently running v5.0.0.2012 My current Target is set to Android 9 which is what my phone runs

Xamarin.Forms 5.0 requires the Target Framework should be Android 10 or greater and uses AndroidX by defult. Have you updated the Xamarin.Forms package on the Android platform project?

If the Target Framework is set to Android 10, we should use AndroidX.Core.Content.FileProvider.GetUriForFile instead. And the android:name's value of the <provide> tag should also be changed to androidx.xxx .

<provider android:name="androidx.core.content.FileProvider" 
          android:authorities="${applicationId}.fileprovider" 
          android:exported="false" 
          android:grantUriPermissions="true">
      <meta-data android:name="android.support.FILE_PROVIDER_PATHS" 
                     android:resource="@xml/file_paths"></meta-data>
</provider>