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;
try
{
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();
try
{
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.