如何检索特定SMS消息的联系详细信息?

时间:2015-12-14 04:54:48

标签: java android

如何获取短信发件人的联系方式?我试图弄清楚如何检索发件人的照片,但当我尝试获取sms消息的发件人详细信息时,它返回NULL或0:

String number = getString(cur.getColumnIndexOrThrow("_id"));

该代码处于while循环中,我在其中获取存储在设备上的所有消息,并尝试提取发件人详细信息以进行处理。

1 个答案:

答案 0 :(得分:1)

 Uri uri = Uri.parse("content://sms/inbox");
            Cursor c = getActivity().getContentResolver().query(uri, null, null, null, null);

            while (c.moveToNext()) {

                String body = c.getString(c.getColumnIndexOrThrow("body")).toString();
                String address = c.getString(c.getColumnIndexOrThrow("address")).toString();
                String contactId = c.getString(c.getColumnIndexOrThrow("person")).toString();

                if (contactId != null) {

                    InputStream inputStream = ContactsContract.Contacts
                            .openContactPhotoInputStream(getActivity().getContentResolver(), ContentUris
                                    .withAppendedId(
                                            ContactsContract.Contacts.CONTENT_URI,
                                            Long.valueOf(contactId)));

                    if (inputStream != null) {
                        Bitmap photo = BitmapFactory.decodeStream(inputStream);
                    }

                    c.moveToNext();
                }
                c.close();