1

I did get the information on how to retrieve the text and the image for the mms sent from this link: How to Read MMS Data in Android?.

But I am not sure how to retrieve the date for the mms that was sent.

I know I have to look into content://mms and not in content://mms/part.

This is the Mothod to retrieve the mms text:

private String getMmsText(String id) {
        Uri partURI = Uri.parse("content://mms/part/" + id);
        InputStream is = null;
        StringBuilder sb = new StringBuilder();
        try {
            is = getContentResolver().openInputStream(partURI);
            if (is != null) {
                InputStreamReader isr = new InputStreamReader(is, "UTF-8");
                BufferedReader reader = new BufferedReader(isr);
                String temp = reader.readLine();
                while (temp != null) {
                    sb.append(temp);
                    temp = reader.readLine();
                }
            }
        } catch (IOException e) {
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
        return sb.toString();
    }

and then, in the onCreate method, I use this code to get the info:

Cursor cursor = getContentResolver().query(uri, null, selectionPart,
                null, null);
        if (cursor.moveToFirst()) {
            do {
                String partId = cursor.getString(cursor.getColumnIndex("_id"));
                String type = cursor.getString(cursor.getColumnIndex("ct"));
                if ("text/plain".equals(type)) {
                    String data = cursor.getString(cursor
                            .getColumnIndex("_data"));

                    if (data != null) {
                        // implementation of this method above
                        body = getMmsText(partId);
                    } else {
                        body = cursor.getString(cursor.getColumnIndex("text"));
                    }
                }
            } while (cursor.moveToNext());
        }

        try {


            main.setText(body);
            img.setImageBitmap(bitmap);
        } catch (Exception e) {

            e.printStackTrace();
        }

I just want to know where can I make changes to get the date value.

Some info will be really helpful.

Community
  • 1
  • 1
mike20132013
  • 5,357
  • 3
  • 31
  • 41

1 Answers1

7

I'm not overly familiar with MMS's, but I'd imagine something like this would at least get you started

Cursor cursor = activity.getContentResolver().query(Uri.parse("content://mms"),null,null,null,date DESC);
count = cursor.getCount();
if (count > 0) 
{
    cursor.moveToFirst();
    long timestamp = cursor.getLong(2);
    Date date = new Date(timestamp);
    String subject = cursor.getString(3);
}

It's completely untested of course, but should point you in the right direction. Hope this helps!

Edit After doing a bit of reading, there used to be (possibly still is) a "bug" with the timestamp in MMS messages, when retrieving the data. If you end up with a silly value (like the epoch), you'll have to * 1000 before using it. Just an aside :) I.e.:

long timestamp = (cursor.getLong(2) * 1000);
laminatefish
  • 5,197
  • 5
  • 38
  • 70
  • Thank for the info. This was really helpful..:). The code is working but the year is showing as 1970 for some reason. – mike20132013 Jan 30 '14 at 15:42
  • 1
    Ah. See my edit :) Simply change to: long timestamp = (cursor.getLong(2) * 1000); - Should sort it out :) – laminatefish Jan 30 '14 at 15:42
  • This has been tested as working. What is your particular problem? Maybe start another question and post relevant code, if none of the suggestions or other similar questions/answers help? – laminatefish Aug 11 '14 at 11:38
  • Sorry for reviving old thread. But not working for me. I opened a new issue here http://stackoverflow.com/questions/36228389/mms-date-is-alwasy-wrong – KVISH Mar 25 '16 at 22:09