MMS in Android. Part 2. Working with MMS storage

August 15, 2011 § 24 Comments

Getting list of MMS from the storage

Multimedia Message consists of MMS and MMParts. First contains message information (date, threadId, message box etc). Lasts contain content (text, images, video, audio). To get whole MMS data we must get MMS and then retrieve all MMS Parts of that message.

  • Getting MMS
     context.getContentResolver().query(
        Uri.parse("content://mms"),
        new String[] {"_id", "date", "msg_box", "read", "sub", "thread_id"},
        null,
        null,
        "date");

    Here we have queried all mms. If we want to get all messages from/to certain address we must specify thread_id and put third parameter like this “thread_id=3″ for recipient with threadId = 3.

  • Retrieving MMS Parts
     String where = "mid"+ " = '" + messageId + "'";
    context.getContentResolver().query(
        Uri.parse("content://mms/part"),
        new String[] {"_id", "ct", "_data", "text", "cl"},
        where,
        null,
        null);

    Here messageId is id of message at content://mms uri.

Inserting/updating/deleting mms data to/at/from storage

To insert MMS we should:

  • Insert message
    ContentValues values = new ContentValues();
    values.put("thread_id", threadId);
    values.put("date", date/1000);
    values.put("read", true); //read status
    values.put("sub", text); //mms subject
    values.put("msg_box", 4); //message box. in this case outbox
    Uri mmsUri = context.getContentResolver().
                 insert(Uri.parse("content://mms"), values);
  • Insert part
    ContentValues mmsPartValue = new ContentValues();
    mmsPartValue.put(MMS_PART_CONTENT_TYPE, mime); //mime; for example image/jpeg
    //id of MMS at content://mms
    String messageId = mmsUri.getLastPathSegment().trim();
    Uri partUri = Uri.parse("content://mms/" + messageId + "/part");
    Uri mmsPartUri = context.getContentResolver().insert(partUri, mmsPartValue);
  • Add data to part
    //mmsPartUri - uri to added mms part
    //uriToData - uri to content you want to add to mms part
    (uri of amedia file in the storage, mostly in the gallery).
    OutputStream os = context.getContentResolver().openOutputStream(mmsPartUri);
    InputStream is = context.getContentResolver().openInputStream(uriToData);
    byte[] buffer = new byte[256];
    for (int len = 0; (len = is.read(buffer)) != -1; ) {
        os.write(buffer, 0, len);
    }

Delete and update requests have absolutely the same principle.

About these ads

§ 24 Responses to MMS in Android. Part 2. Working with MMS storage

  • monir016782 says:

    Thanks you very very much. I am searching this tutorial for a long time. Specially to insert mms part.

    I will knock you soon.

    BR.
    Limon.

  • monir016782 says:

    Hi maxim,
    Could you please provide me a complete code to create a mms programmatically ?

    I don’t figure it out yet:

    //mmsPartUri – uri to added mms part
    //uriToData – uri to content you want to add to mms part

    Please explain.

    thanks.

  • suporna says:

    Nice tutorial, thanks.

  • maximbogatov says:

    Hi Limon.

    Actually I have missed the mmsPartUri in the code. It is returned by mmsPart insert line. I have added it. Thanks for the question.

    mmsPartUri – uri of mmsPart. Firstly you insert mmsPart to storage. And then you write data to it.

    uriToData – the uri of a media file (imge, video, audio) in your storage (mostly in the gallery).

    If you have any related questions feel free to ask. I will do my best to clarify it.

  • Tony says:

    When you need to insert the MMS part, you need a message id?

    If so, how do you create that id?

    Thanks,
    T.

    • maximbogatov says:

      Yes, you should add part to mms through its id otherwise it would not be connected with anything.
      You can get it this way:
      String messageId = mmsUri.getLastPathSegment().trim();

  • Jenny says:

    When I try making an insert in this manner, everything is added just fine in the mmssms database. However, the MMS is never shown in the Messaging app. Am I missing something?

  • Peter J says:

    Hi
    Im tring to insert a new mms as you did, but I cant see it in the Messages application. It only appears when I list them programatically.

    Do you have an idea what can be wrong? What should I set for the thread_id?

    Thanks in advance.

    • Henrik says:

      Did anyone solve the issue with the MMS nt showing in messages?

      I have the same issue. I wonder if it might have something to do with the application/smil part needed to be added as well.

    • kid says:

      Hi
      Did you resolve this problem?
      I meet this problem, too. Can you help me?

    • kid says:

      Hi.
      Did you resolve this problem?
      I meet this, too. Can you help me?

  • dada says:

    hi i am getting this error when trying to insert sqlite returned: error code = 1, msg = near “/”: syntax error, db=/data/data/com.android.providers.telephony/databases/telephony.db

    my code is

    ContentValues values = new ContentValues();
    values.put(“thread_id”, 4);
    //String date=”16-03-2012″;
    values.put(“date”, 16-03-2012/1000);
    values.put(“read”, true); //read status
    values.put(“sub”,”bye”); //mms subject
    values.put(“msg_box”, 4); //message box. in this case outbox
    Uri mmsUri = getBaseContext().getContentResolver().
    insert(Uri.parse(“content://mms”), values);
    //Insert part of mms
    ContentValues mmsPartValue = new ContentValues();
    String MMS_PART_CONTENT_TYPE=”image/png”;
    String mime=”er.png”;
    mmsPartValue.put(MMS_PART_CONTENT_TYPE, mime); //mime; for example image/jpeg
    //id of MMS at content://mms
    String med = mmsUri.getLastPathSegment().trim();
    Uri partUri = Uri.parse(“content://mms/”+med+”/part”);
    Uri mmsPartUri = getContentResolver().insert(partUri, mmsPartValue);
    ///Add data to part of mms
    //mmsPartUri – uri to added mms part
    //uriToData – uri to content you want to add to mms part
    //(uri of amedia file in the storage, mostly in the gallery).
    Uri urimiuyt = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), “er.png”));

    System.out.println(mmsPartUri);
    InputStream is;
    try {
    OutputStream os = getBaseContext().getContentResolver().openOutputStream(mmsPartUri);
    is =getContentResolver().openInputStream(urimiuyt);
    byte[] buffer = new byte[256];
    for (int len = 0; (len = is.read(buffer)) != -1; ) {
    os.write(buffer, 0, len);}
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

  • dada says:

    hai i have a doubt if we insert mms in db it is going to deliver to address r not i checking in the emulator dats why i am asking to you

  • dada says:

    hi in your tutorial you have not mention the phoneno of the sender where it should be specified in the code

  • dada says:

    hai maximbogatov please tell me how can i add sender phone number also while inserting. i have tried this code
    ContentValues val = new ContentValues();
    val.put(“msg_id”, 16);
    val.put(“address”, “8958856009″);
    Uri mmsadd = getBaseContext().getContentResolver().
    insert(Uri.parse(“content://mms/addr”), val);
    but it is giving me error as invalid insert

    • maximbogatov says:

      Unfortunately you can’t. Actually there is no even real (100%) way to fetch this number while most of mobile operators doesn’t write it to a sim card.

      • dada says:

        sorry i did not get you i am asking about inserting the phone no for whom we want to send

  • gk says:

    Strange, the Insert Part does not work for me, I use the mms/part through ContentValues and insert(uri,values), I do not add any image, but retrieving the data in mms/part after the insert shows no data was added in the fields I specified, eg. mid,ct. Any ideas? Really love to get this to work!!!

  • Aikito Atasan says:

    Do you have some news for the native message display after inserting into the database??

    jenny issu :

  • Jens says:

    This is brilliant! Thanks for the code snippets, helped me a lot! Keep up the good work.

  • sareesh says:

    hi maxim
    i am developing an mms application like this.
    For an upgradation i need to send mms automatically, that means when i disconnect the phone call,it will record and send to another number.
    is it possible?, if yes plz reply…

  • aamir says:

    hi! thanks for the nice tutorial,i want to delete MMS of a perticular phone number,i have tried with following code this.getContentResolver().delete(
    Uri.parse(“content://mms-sms/conversations/” + ThreadId),
    “address=?”, new String[] { “contact number” });
    but it seems not working
    can you please tell me how can i delete mms of a perticular phone number?
    thanks and regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading MMS in Android. Part 2. Working with MMS storage at maxim bogatov.

meta

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: