如何阅读彩信数据在Android中? [英] How to Read MMS Data in Android?

查看:287
本文介绍了如何阅读彩信数据在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读彩信数据我已经看到了部分表中的 mmssms.db ,其中条目存储彩信;我使用的是光标,我想知道相应的 URI ;我使用的内容://彩信,短信/通话,并在列名的地址(送),文本或主题和图像的数据列名

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 URI; I am using "content://mms-sms/conversations" and the Column names of "Address"(Sent to), "Text" or "Subject" and "Data" column name of image.

我已经看到 mmssms.db 的模式和他们对部分表列。

I have seen the schema of mmssms.db and Their Column of part Table.

推荐答案

这是找对本文档的一种困难的,所以我会在这里收集所有的信息,我已经找到。如果你是在匆忙或只是不喜欢阅读,跳转到如何从短信中获取数据部分。

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.

这是的<一个包含URI href="http://android.git.kernel.org/?p=platform/packages/providers/TelephonyProvider.git;a=blob;f=src/com/android/providers/telephony/MmsSmsProvider.java;h=e379c454ee203cb02aee59737b58bea24ac405cf;hb=HEAD">Mms和短信提供商 ......这使我们能够查询MMS和SMS数据库的同时,和它们混合在一个单独的线程(即所谓的对话的)。

This 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).

为什么是重要的内容://彩信,短信/通话 URI?嗯,这是越来越彩信和短信的标准方法;比如,当你收到短信,点击通知栏上,它将发送一个广播的意图是这样的:内容://彩信,短信/通话/ XXX ,其中 XXX 是对话的ID。

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.

您必须做的唯一一件事就是查询内容://彩信,短信/通话开放的:

The only thing you have to do is to query the content://mms-sms/conversations Uri:

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

注意:通常情况下,当你调用查询键,想返回你可以通过所有列预测参数。但是,你不能做到这一点与这个供应商,所以这就是为什么我使用 *

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:

  • _id 是消息的ID。 船长明显的救援?的不是真的。此ID可用于通过获取详细信息,或者内容://短信内容://彩信
  • 日期没有解释必要的。
  • 的thread_id 是对话的ID
  • 在这次谈话的最后一个短信的内容。如果它是一个彩信,即使它有一个文本部分,这将是
  • _id is the ID of the message. Captain obvious to the rescue? Not really. This ID can be used to retrieve detailed information using either content://sms or content://mms.
  • date no explanation needed.
  • thread_id is the ID of the conversation
  • body The content of the last SMS on this conversation. If it's an MMS, even if it has a text part, this will be null.

注意:如果您查询内容://彩信,短信/通话将返回不同的对话,其名单 _id 是最后一个短信或彩信中的每个会话。如果查询内容://彩信,短信/通话/ XXX 将返回每个SMS和/或MMS上其ID的谈话 XXX

Note: if you query 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.

通常情况下,你会想知道你正在处理哪些类型的消息。文件说:

Usually, you will want to know which type of message you are handling. Documentation says:

有一个虚拟列,    MmsSms.TYPE_DISCRIMINATOR_COLUMN ,可   在投影请求要   查询。它的值是彩信或   SMS,这取决于是否   消息重新$ P $由行psented是   彩信或短信,   分别。

A virtual column, MmsSms.TYPE_DISCRIMINATOR_COLUMN, may be requested in the projection for a query. Its value is either "mms" or "sms", depending on whether the message represented by the row is an MMS message or an SMS message, respectively.

我认为这是指的<一个href="http://www.google.com/$c$csearch#cZwlSNS7aEw/frameworks/base/core/java/android/provider/Telephony.java&exact_package=android&l=1591">this可变 ......但是我还没有能够使它发挥作用。如果有,请告诉我如何或编辑这篇文章。

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:

ContentResolver contentResolver = getContentResolver();
final String[] projection = new String[]{"_id", "ct_t"};
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor query = contentResolver.query(uri, projection, null, null, null);
if (query.moveToFirst()) {
    do {
        String string = query.getString(query.getColumnIndex("ct_t"));
        if ("application/vnd.wap.multipart.related".equals(string)) {
            // it's MMS
        } else {
            // it's SMS
        }
    } while (query.moveToNext());
}

如何从短信中获取数据

所以,你有短信的ID,那么你所要做的唯一事情是:

How to get data from a SMS

So you have the ID of the SMS, then the only thing you have to do is:

String selection = "_id = "+id;
Uri uri = Uri.parse("content://sms");
Cursor cursor = contentResolver.query(uri, null, selection, null, null);
String phone = cursor.getString(cursor.getColumnIndex("address"));
int type = cursor.getInt(cursor.getColumnIndex("type"));// 2 = sent, etc.
String date = cursor.getString(cursor.getColumnIndex("date"));
String body = cursor.getString(cursor.getColumnIndex("body"));

如何从MMS数据获取数据?

彩信是有点不同。它们可与不同的部分(文本,音频,图像等)进行构建所以在这里将看到如何单独获取的各种数据。

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.

因此​​,让我们觉得我们有彩信ID在 mmsId 变量。我们可以通过使用内容获取有关该彩信的详细信息:// MMS / 提供商:

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:

Uri uri = Uri.parse("content://mms/");
String selection = "_id = " + mmsId;
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);

不过,唯一有趣的列是 1 如果消息已被读取。

However, the only interesting column is read which is 1 if the message has already been read.

在这里,我们必须使用内容:// MMS /部分 ...例如:

Here we have to use content://mms/part... for instance:

String selectionPart = "mid=" + mmsId;
Uri uri = Uri.parse("content://mms/part");
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"));
            String body;
            if (data != null) {
                // implementation of this method below
                body = getMmsText(partId);
            } else {
                body = cursor.getString(cursor.getColumnIndex("text"));
            }
        }
    } while (cursor.moveToNext());
}

它可以包含文本的不同部分...但通常它会是唯一的一个。所以,如果你想删除的循环,将工作大部分时间。这是怎样的 getMmsText 的方法是这样的:

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();
}

如何从彩信获得的图像

这是比获得的文本部分相同的......唯一不同的是,你将寻找一个不同的MIME类型:

How to get image from MMS

It's the same than getting the text part... the only difference is that you will be looking for a different mime-type:

String selectionPart = "mid=" + mmsId;
Uri uri = Uri.parse("content://mms/part");
Cursor cPart = getContentResolver().query(uri, null,
    selectionPart, null, null);
if (cPart.moveToFirst()) {
    do {
        String partId = cPart.getString(cPart.getColumnIndex("_id"));
        String type = cPart.getString(cPart.getColumnIndex("ct"));
        if ("image/jpeg".equals(type) || "image/bmp".equals(type) ||
                "image/gif".equals(type) || "image/jpg".equals(type) ||
                "image/png".equals(type)) {
            Bitmap bitmap = getMmsImage(partId);
        }
    } while (cPart.moveToNext());
}

这是怎么了 getMmsImage 的方法是这样的:

This is how the getMmsImage method looks like:

private Bitmap getMmsImage(String _id) {
    Uri partURI = Uri.parse("content://mms/part/" + _id);
    InputStream is = null;
    Bitmap bitmap = null;
    try {
        is = getContentResolver().openInputStream(partURI);
        bitmap = BitmapFactory.decodeStream(is);
    } catch (IOException e) {}
    finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {}
        }
    }
    return bitmap;
}

如何获得发件人地址

您将需要使用内容:// MMS / XXX /地址提供商,其中 XXX 是ID彩信:

How to get the sender address

You will need to use the content://mms/xxx/addr provider, where xxx is the id of the MMS:

private String getAddressNumber(int id) {
    String selectionAdd = new String("msg_id=" + id);
    String uriStr = MessageFormat.format("content://mms/{0}/addr", id);
    Uri uriAddress = Uri.parse(uriStr);
    Cursor cAdd = getContentResolver().query(uriAddress, null,
        selectionAdd, null, null);
    String name = null;
    if (cAdd.moveToFirst()) {
        do {
            String number = cAdd.getString(cAdd.getColumnIndex("address"));
            if (number != null) {
                try {
                    Long.parseLong(number.replace("-", ""));
                    name = number;
                } catch (NumberFormatException nfe) {
                    if (name == null) {
                        name = number;
                    }
                }
            }
        } while (cAdd.moveToNext());
    }
    if (cAdd != null) {
        cAdd.close();
    }
    return name;
}

最后的想法

  • 在不明白为什么谷歌,与数千万元,不交的学生或其他人来记录这个API。你必须检查源$ C ​​$ C知道它是如何工作的,并且这更糟糕的是,他们不公开那些在数据库中的列使用的常数,因此,我们不得不手工编写它们。
  • 对于其他类型的数据里面彩信你可以申请上述了解到相同的想法......这是知道的MIME类型只是一个问题。
  • Final thoughts

    • Can't understand why Google, with those thousands of millions of dollars, don't pay a student or someone else to document this API. You have to check the source code to know how it works and, which is worse, they don't make public those constants used in the columns of the database, so we have to write them manually.
    • For other kind of data inside an MMS you can apply the same idea learned above... it's just a matter of knowing the mime-type.
    • 这篇关于如何阅读彩信数据在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆