How to Read MMS Data in Android?

前端 未结 5 1365
梦毁少年i
梦毁少年i 2020-11-22 13:52

I want to read MMS data I have seen the part table in the mmssms.db where the mms entries are stored; I am using a cursor and I want to know the appropriate

5条回答
  •  眼角桃花
    2020-11-22 14:20

    I had to make some modifications in order to get this to work for me.

    1. When I retrieve the cursor.getString(cursor.getColumnIndex("type")) from the mms-sms/conversations content, ("content://mms-sms/conversations/") I test the value of the "type" field for null. If the variable is null - i.e.

      String otype = c.getString(c.getColumnIndex("type"));
      if(otype != null) {
          //this is an sms - handle it...
      

      the message is an SMS, else it is an MMS. For MMS's you have to test for both mime types as follows:-

      if (("application/vnd.wap.multipart.related".equalsIgnoreCase(msg_type)
          ||"application/vnd.wap.multipart.mixed".equalsIgnoreCase(msg_type))
          && !id.equalsIgnoreCase(lastMMSID)) {
               //this is a MMS - handle it...
      
    2. When you use a ContentObserver to monitor the message content for changes, it fires several notifications for the same message. I use a static variable - in my case lastMMSID - to keep track of the message.
    3. This code works well to retrieve the content of both Inbound and Outbound messages. It is important to iterate through all the records that are returned by the "content://mms/part/" uri in order to get to the content - text and/or attachments - of the MMS.
    4. The only way that I could find that works pretty well to differentiate between inbound and outbound MMS's, is to test the null status of the "m_id" field of the mms-sms/conversations content.

      String m_id = c.getString(c.getColumnIndex("m_id"));
      String mDirection = m_id == null? "OUT": "IN";
      

    A final thought on how to get the Address Field. For some reason the Address Content does not like to be queried with a {" * "} parameter, but this works:-

    final String[] projection = new String[] {"address", "contact_id", "charset", "type"};
    

    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:-

    private String getANumber(int id) {
        String add = "";
        final String[] projection = new String[] {"address","contact_id","charset","type"};
        final String selection = "type=137 or type=151"; // PduHeaders
        Uri.Builder builder = Uri.parse("content://mms").buildUpon();
        builder.appendPath(String.valueOf(id)).appendPath("addr");
    
        Cursor cursor = context.getContentResolver().query(
            builder.build(),
            projection,
            selection,
            null, null);
    
    if (cursor.moveToFirst()) {
              do {
                  String add = cursor.getString(cursor.getColumnIndex("address"));
                  String type: cursor.getString(cursor.getColumnIndex("type"));
              } while(cursor.moveToNext());
          }
          // Outbound messages address type=137 and the value will be 'insert-address-token'
          // Outbound messages address type=151 and the value will be the address
          // Additional checking can be done here to return the correct address.
          return add;
    }
    

    To all the brave warriors who have gone before me in this post - I thank thee from the bottom of my heart!

提交回复
热议问题