Hello,
I'd like to read an SMS and/or MMS message on an Android device only,
Firstly, before you read the SMS/MMS, you need to add the permission in the <manifest>
tag of Platforms/Android/Resources/AndroidManifest.xml
and request permission.
Read the SMS and request permission, you can refer to this link: Access and read all SMS on Android phone with .NET MAUI
Read the MMS, you can get ID by MMS Attachment Table, then you can get Image stream by id in MMS inbox. Because this MMS file type will be text, image or others, we need to judge the file type.
In the end, we can get the stream by Stream resStream = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.ContentResolver.OpenInputStream(PartURI1);
, if you have many images, you can save this image stream in the List<Stream>
.
var res = await CheckAndRequestSMSPermission();
if (res.Equals(PermissionStatus.Granted))
{
#if ANDROID
Android.Net.Uri CONTENT_URI = Android.Net.Uri.Parse("content://mms/inbox");
//Check your MMS inbox
Android.Net.Uri CONTENT_URI_PART = Android.Net.Uri.Parse("content://mms/part");
//Check your MMS inbox
Android.Database.ICursor cursor = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.ContentResolver.Query(CONTENT_URI, null, null, null, null);
List<int> mmsIDs = new List<int>();
List<string> mmsSubjects = new List<string>();
if (cursor.MoveToFirst())
{
do
{
int id = cursor.GetInt(cursor.GetColumnIndex("_id"));//MMS Id
mmsIDs.Add(id);
string subject = cursor.GetString(cursor.GetColumnIndex("sub")); //MMS theme
mmsSubjects.Add(subject);
} while (cursor.MoveToNext());
}
if (mmsIDs.Count > 0)
{
foreach (int id in mmsIDs)
{
string selection = "mid=" + id; //this key is the mms id。
Android.Database.ICursor cPart = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.ContentResolver.Query(CONTENT_URI_PART, null, selection, null, null);
Android.Net.Uri partURI = Android.Net.Uri.Parse("content://mms/part/" + "_id");
List<Stream> allMMSFileStream = new List<Stream>();
String bodyStr = "";
String[] coloumns = null;
String[] values = null;
while (cPart.MoveToNext())
{
coloumns = cPart.GetColumnNames();
if (values == null)
values = new string[coloumns.Length];
for (int i = 0; i < cPart.ColumnCount; i++)
{
values[i] = cPart.GetString(i);
}
if (values[3].Equals("image/jpeg") || values[3].Equals("image/bmp") || values[3].Equals("image/gif") || values[3].Equals("image/jpg") || values[3].Equals("image/png"))
{ //judge the file type, if the file type is image.
Android.Net.Uri PartURI1 = Android.Net.Uri.Parse("content://mms/part/" + values[0]);
Stream resStream = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.ContentResolver.OpenInputStream(PartURI1);
if(resStream != null)
{
//save the Image Stream to List
allMMSFileStream.Add(resStream);
}
//I make a ImageView to make a test.
myImage.Source = ImageSource.FromStream(()=> resStream);
}
}
}
}
#endif
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.