Send and read MMS with sendMultimediaMessage

1.9k views Asked by At

I know there are a ton of similar, older questions on SO about this, but I'm not having any luck finding what I need.

Sending

I want to be able to send an MMS message using Android's SmsManager.sendMultimediaMessage function. More specifically, I want to provide a phone number, and either a string of text or the URI of an image, and send the message, then have the result be broadcast as a PendingIntent.

I've probably looked at a dozen SO questions about this, with no luck.

As an example, here's how I implemented the SMS version of this. Notice how the method stores the message in the database, then scans it and returns the ID.

public int sendSms(String number, String text);
    ContentValues values = new ContentValues();
    long threadId = getOrCreateThreadId(getApplicationContext(), number);
    values.put(Telephony.Sms.ADDRESS, number);
    values.put(Telephony.Sms.DATE, System.currentTimeMillis());
    values.put(Telephony.Sms.DATE_SENT, System.currentTimeMillis());
    values.put(Telephony.Sms.READ, 1);
    values.put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_OUTBOX);
    values.put(Telephony.Sms.THREAD_ID, threadId);
    values.put(Telephony.Sms.BODY, text);
    getContentResolver().insert(Telephony.Sms.Inbox.CONTENT_URI, values);
    Cursor c = getContentResolver().query(Telephony.Sms.CONTENT_URI, null, null, null, "_id desc");
    int id = -1;
    if (c.moveToFirst())
        id = c.getInt(c.getColumnIndexOrThrow(BaseColumns._ID));
    c.close();
    Log.i("MessagingService", "Sending message: " + text);
    ArrayList<String> parts = SmsManager.getDefault().divideMessage(text);
    ArrayList<PendingIntent> pendingIntents = new ArrayList<>();
    for (String i : parts) {
        Intent intent;
        if (id != -1) {
            intent = new Intent(MainActivity.MSG_STATUS_CHANGE);
            intent.putExtra("id", id);
        } else {
            intent = new Intent(MainActivity.RUN_CODE);
            intent.putExtra("type", "reload-thread");
            intent.putExtra("content", String.valueOf(threadId));
        }
        pendingIntents.add(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
    }
    StatusChangeReceiver.pendingPartCounts.put(id, parts.size());
    SmsManager.getDefault().sendMultipartTextMessage(number, null, parts, pendingIntents, null);
    return id;
}

I want to do something similar to this using sendMultimediaMessage. Note that I'm targing Android 5.0 (API level 21), but I have a custom implementation of getOrCreateThreadId for compatibility.

Reading

Also, is reading MMS messages similar to reading SMS ones? If not, how would I go about doing this?

0

There are 0 answers