How to Read MMS Data in Android?
It's kind of difficult to find documentation about this, so I will collect here all information I have found. If you are in a rush or just don't like to read, jump to the How to get data from a SMS section.
content://mms-sms/conversationsThis is the URI of the Mms and SMS provider... which allows us to query the MMS and SMS databases at the same time, and mix them in a single thread (which are called conversations).Why is it important the content://mms-sms/conversations uri? Well, that's the standard way of getting MMS and SMS messages; for instance, when you receive a SMS and click on the notification bar, it will send a broadcast intent like this: content://mms-sms/conversations/XXX , where XXX is the id of the conversation.Get a list of all conversationsThe only thing you have to do is to query thecontent://mms-sms/conversations Uri:
Note: usually, when you call query and want to return all columns you can pass null as the projection parameter. However, you cannot do that with this provider, so that's why I'm using * .Now you can loop through the Cursor as usual. These are the more important columns you would want to use:
content://mms-sms/conversations it will return a list of different conversations whose _id is the last SMS or MMS in each conversation. If you query content://mms-sms/conversations/xxx it will return each SMS and/or MMS on the conversation whose ID is xxx .How to differentiate between SMS and MMSUsually, you will want to know which type of message you are handling. Documentation says:
A virtual column,
I think it's referring to this variable... however I have not been able to make it work. If you have please tell me how or edit this post.So far this is what I have done and it seems to work but there must be better ways:
How to get data from a SMSSo you have the ID of the SMS, then the only thing you have to do is:
How to get data from a MMS data?MMSs are a little bit different. They can be built with different parts (text, audio, images, etc.); so here will see how to retrieve each kind of data separately.So let's guess we have the MMS id in the mmsId variable. We can get detailed information about this MMS by using the content://mms/ provider:
However, the only interesting column is read which is 1 if the message has already been read.How to get text content from MMSHere we have to usecontent://mms/part ... for instance:
It could contain different parts of text... but usually it'd be only one. So if you want to remove the loop it will work most of the times. This is how the getMmsText method looks like:
How to get image from MMSIt's the same than getting the text part... the only difference is that you will be looking for a different mime-type:
This is how the getMmsImage method looks like:
How to get the sender addressYou will need to use thecontent://mms/xxx/addr provider, where xxx is the id of the MMS:
Final thoughts
| |||||||||||||||
|
The answer by Christian is excellent. However, the method for getting the sender's address did not work for me. The Long.parseLong statement doesn't do anything except possibly throw an exception and new String(...) ?.
On my device the cursor count is 2 or more. The first typically has a "type" of 137 and the others have a "type" of 151. I cannot find where this is documented, but one can deduce 137 is "from" and 151 is "to". Thus, if I run the method as is, I do not get an exception, and it returns the last row, which is a recipient and only one of several in many cases. Also AFAICT the selection is not necessary as all the rows have the same msg_id. However, it doesn't hurt. This is what works for me to get the sender's address:
I didn't care about whether it is all numeric, but I included a way to eliminate everything but numerals as a comment if that is desired. It can easily be modified to return all the recipients, as well.I assume it worked for him. It looks like it would give the right answer if the exception occurred on the first row. | |||
|
Take a look at this. Worked well for me.
| |||||
|
The answer given above for getting the getMMSAddress() should not contain the loop while (cursor.moveToNext());. It should only extract the address from the first element in the cursor. For some reason that is unknown to me, this cursor has more than one record. The first one contains the Sender's address. The other elements of the cursor beyond the first one, contain the receiver's address. Thus the code as is return the receivers address and not the sender address.
This has been very helpful for cracking open the contents of an MMS. | |||
I had to make some modifications in order to get this to work for me.
If it is an outbound message, the "type" to look for will be 151. For an inbound message, the "type" will be 137. A fully functional piece of code will look something like this:-
To all the brave warriors who have gone before me in this post - I thank thee from the bottom of my heart! | ||||
mmssms.db
database is part of the firmware and is not accessible by Android applications. Thecontent://mms-sms/conversations
content provider is not part of the SDK and should not be accessed by Android applications. – CommonsWare Jun 10 '10 at 11:12