0

I am getting the exception error: 'Unable to get provider androidx.core.content.FileProvider: java.lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data

Here is a bit 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("11111111111", 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." + "sendMe" + ".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);
                //    Android.Net.Uri contentURI = Android.Net.Uri.FromFile(testFile).Au

                   /*     Android.Net.Uri contentURI = (new Android.Net.Uri.Builder())
                       .Authority(authString)
                       .Path(cacheFilePath)
                       .Scheme(ContentResolver.SchemeContent)
                       .Build();
                       */

                    /*
                     * = (new Android.Net.Uri.Builder())
                       .Authority(ctx.PackageName + ".fileprovider")
                       .Path(cacheFilePath)
                       .Scheme(ContentResolver.SchemeContent)
                       .Build();
                       */

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

Also, here is my Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.janinesafety2" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
    <application android:label="janinesafety2.Android">
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.suport.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"
            />
    </provider>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_USER_DICTIONARY" />
    <uses-permission android:name="android.permission.WRITE_USER_DICTIONARY" />
</manifest>

and my Provider_path

<?xml version="1.0" encoding="utf-8" ?>
<paths>
  <external-path
    name="external_file"
    path="./"
  />
</paths>

I have used these as resources to assist me in debugging:

androidx FILE_PROVIDER_PATHS Unable to get provider androidx.core.content.FileProvider: java.lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data?

And unfortuntaly, I am still having issues with the expection. From what I can tell I am doing the exact thing everyone is saying.

Is there anyone there that can take a second a look at my code? Maybe I missed something.

Thank you.

philm
  • 797
  • 1
  • 8
  • 29

1 Answers1

0

You could refer to the code below.

1.Add the following to your AndroidManifest.xml inside the tags. YOUR_APP_PACKAGE_NAME must be set to your app package name.

 <application>
  <provider android:name="android.support.v4.content.FileProvider" 
      android:authorities="YOUR_APP_PACKAGE_NAME.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>

2.Add a new folder called xml into your Resources folder and add a new XML file called file_paths.xml. Add the following code:

<?xml version="1.0" encoding="utf-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" 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>

I added all the tags. You could get from the link below. FileProvider - IllegalArgumentException: Failed to find configured root

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • So I am running Xamarin forms 5 and in this version, they use the androidx library. Also, I copied and pasted your code for the File-paths.xml file and I got a number of build errors. – philm Mar 22 '21 at 17:27
  • I missed the closed tab `` for the file_paths.xml. What is the build errors? I would check the provider for the androidx library later. – Wendy Zang - MSFT Mar 23 '21 at 07:14
  • I ended up solving the issue. There was a type in one of my files. Once the type was fixed, I was able to get it working. There is another question related to this project that I need some assistance on. Would you be able to take a look? https://stackoverflow.com/questions/66752698/need-help-unable-to-send-mms-xamarin-forms-android – philm Mar 23 '21 at 14:19