In Xamarin.Android, I have setup a custom ContentProvider
that serves the PDU file to the system when sending an MMS:
[ContentProvider(new[] { Authority }, GrantUriPermissions = true)]
internal class MmsUploadContentProvider : ContentProvider
{
internal const string Authority = "my.package.name.mmsUploads";
public override ParcelFileDescriptor OpenFile(AndroidUri uri, string mode)
{
File uploadsDirectory = GetMmsUploadsDirectory(this.Context);
File file = new File(uploadsDirectory, uri.LastPathSegment);
ParcelFileDescriptor result = ParcelFileDescriptor.Open(file, ParcelFileMode.ReadOnly);
return result;
}
}
I call SendMultimediaMessage
like so:
Uri contentProviderUri = Uri.Parse($"content://{MmsUploadContentProvider.Authority}/{pduFileName}");
smsManager.SendMultimediaMessage(this.Context, contentProviderUri, null, null, statusPendingIntent);
However, when my BroadcastReceiver
is called with the result of the request, it receives a MMS_ERROR_IO_ERROR result code. In the documentation, it talks about an issue reading the PDU, so I assume there is something wrong with my ContentProvider
. When I debug, OpenFile
gets called in my ContentProvider
, and a proper ParcelFileDescriptor
gets returned just fine.
Could someone point me in the right direction to fix this? It used to work some years ago, but something must have changed in the Android APIs, causing this to break.
For those still interested, ultimately the issue was not the
ContentProvider
. The issue was that the file being sent was slightly larger than what MMS allowed in this case. This will throw an MMS_ERROR_IO_ERROR.The solution I came up with was to first take a look at what MMS_CONFIG_MAX_MESSAGE_SIZE returned, and gradually compress any media files until the message size went under that limit. I have witnessed that that limit might be lower than the actual maximum size allowed to be sent: I was able to send messages larger than this limit at times. To be on the safe side, I still recommend compressing the MMS up to the limit specified by MMS_CONFIG_MAX_MESSAGE_SIZE anyway.