MMS Date is alwasy wrong

给你一囗甜甜゛ 提交于 2019-12-23 17:49:25

问题


I'm reading SMS and MMS data from my Galaxy S6. All of the SMS messages have a date field like this 1456252633000. The number is the unix timestamp * 1000. The MMS messages that I sent (not received) have date fields like so: 1440628863 the timestamp is proper in unix time format. However, the MMS messages I receive have timestamp completely off, like this: 1454881665.

The two last timestamps above are consecutive, but the second one shows as though it's months before. Why is this so? Is there something I'm doing wrong here?

EDIT

I read this (How do the retrieve the date of the mms from content://mms.) and turns out that my issue is similar. The date is stuck in 1970. But even after converting it to milliseconds (1440185636 * 1000) I still get the wrong date...

EDIT 2

I'm trying to convert using the values as longs, however, still shows the date as months off.

For reference, it was 2/9/2016. I'm getting 8/26/2015 when I do the conversion. The reference timestamp is: 1440603051L * 1000L.

EDIT 3

Here is some code:

    ContentResolver contentResolver = context.getContentResolver();
    Cursor c = contentResolver.query(Telephony.Mms.CONTENT_URI, null, filter, null, null);
    JSONArray array = new JSONArray();

    try {
        if (c.moveToFirst()) {
            do {
                try {
                    String[] cols = c.getColumnNames();
                    String id = c.getString(c.getColumnIndexOrThrow("_id"));
                    JSONObject msg = populateFromMmsPart(id);

                    if (msg != null) {
                        msg.put("id", id);
                        msg.put("read", c.getString(c.getColumnIndexOrThrow("read")).contains("1"));
                        msg.put("time", c.getString(c.getColumnIndexOrThrow("date")));
                        msg.put("m_id", c.getString(c.getColumnIndexOrThrow("m_id")));
                        msg.put("received", c.getString(c.getColumnIndexOrThrow("msg_box")).contains("1"));
                        msg.put("thread_id", c.getString(c.getColumnIndexOrThrow("thread_id")));

                        array.put(msg);
                    }

                } catch (JSONException j) {
                    j.printStackTrace();
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        c.close();
    }

private JSONObject populateFromMmsPart(String msgId) {
    ContentResolver contentResolver = context.getContentResolver();
    Uri messages = Uri.parse("content://mms/part");
    Cursor c = contentResolver.query(messages, null, "_id=" + msgId, null, null);
    JSONObject msg = null;

    if (c.moveToFirst()) {
        try {
            String mid = c.getString(c.getColumnIndex("mid"));
            String type = c.getString(c.getColumnIndex("ct"));

            if (!"application/smil".equals(type)) {
                // Put all the addresses in one place
                msg = new JSONObject();
                msg.put("address", getAddressesForMmsMessages(mid));
                msg.put("mid", mid);
                msg.put("type", type);

                if ("text/plain".equals(type)) {
                    // MMS is just plain text, so get the text
                    String data = c.getString(c.getColumnIndex("_data"));
                    String body;
                    if (data != null) {
                        body = getMmsText(msgId);
                    } else {
                        body = c.getString(c.getColumnIndex("text"));
                    }
                    msg.put("msg", body);
                }
            }
        } catch (JSONException j) {
            j.printStackTrace();
        }
    }

    c.close();
    return msg;
}

Previously I was doing:

ContentResolver contentResolver = context.getContentResolver();
final String[] projection = new String[]{"*"};
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor query = contentResolver.query(uri, projection, "thread_id=109", null, null);

And I was getting the below error:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

回答1:


In the populateFromMmsPart() method, you're querying for records in the part table whose _id matches the message ID from the mms table. However, the _id in the part table is the ID for the part, not the message. The message ID is in the mid column. This is apparently causing a mix-up when a message ID happens to match a part ID for an older message.

Change your query as follows.

Cursor c = contentResolver.query(messages, null, "mid=" + msgId, null, null);

Also, in the populateFromMmsPart() method, you only handle the first row of the returned Cursor. That Cursor is going to have (at least) three rows. One will be application/smil, one will be text/plain, and the other one(s) will be the MIME type(s) for the attachment(s). You need to iterate over that Cursor to get them all. As you have it now, you're just calling c.moveToFirst(), and handling that one row.



来源:https://stackoverflow.com/questions/36228389/mms-date-is-alwasy-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!