简体   繁体   English

如何获取并显示对话联系人的姓名和图像?

[英]How to Fetch and Display Name and Image of Conversation's Contact?

I want to achieve something like in screenshot below, So far I am able to display a list of most recent message with message count, But when I'm trying to get the name of person through "address", I'm getting error " Column address not found ".我想实现如下截图所示的效果,到目前为止,我能够显示带有消息计数的最新消息列表,但是当我试图通过“地址”获取人名时,出现错误“找不到列地址”。 Please help me displaying "contact name with their image".请帮我显示“联系人姓名及其图像”。

我想要达到的目标

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {        
    View view = inflater.inflate(R.layout.MyChatFragment, container, false);
    
    // Inflate the layout for this fragment
    
    ListView lv = (ListView) view.findViewById(R.id.listView2);
     
    ArrayList<String> smsList;        
    smsList = new ArrayList<>();
    ArrayAdapter<String> lva = new ArrayAdapter<String>(getActivity(), 
    android.R.layout.simple_list_item_1, smsList);        
    lv.setAdapter(lva);

    Uri inboxUri = Uri.parse("content://sms/conversations/");
    ContentResolver contentResolver = getActivity().getContentResolver();
    Cursor cursor = contentResolver.query(inboxUri, null, null, null, null);

    if (cursor.moveToFirst()) {
            while (cursor.moveToNext()) {

        String number = cursor.getString(cursor.getColumnIndexOrThrow("address"));
        String message_count = cursor.getString(cursor.getColumnIndexOrThrow("msg_count"));
        String body = cursor.getString(cursor.getColumnIndexOrThrow("snippet"));
        smsList.add("Number: " + number + "\n" + "Message: " + body + "\n" + "Number Of Messages" + 
        message_count );

            }
        }
            return view;
     }

Conversations uri doesn't contain a column like "address".对话 uri 不包含像“地址”这样的列。 You can simply look all columns in specific uri via looping cursor.getColumnName(index) and print it to logcat.您可以通过循环cursor.getColumnName(index)简单地查看特定 uri 中的所有列并将其打印到 logcat。 List of columns into specific uri you can also find in docs .您也可以在docs中找到特定 uri 的列列表。
Basicly, Sms ContentProvider doesn't contain informations about contacts, so you need make a second query to ContactsContract uri, and find a specific contact via his phone number.基本上,Sms ContentProvider 不包含有关联系人的信息,因此您需要对ContactsContract uri 进行第二次查询,并通过他的电话号码找到特定联系人。 Depending of data that you need, there are many uris.根据您需要的数据,有很多 uri。 You can simply link a phone number from sms with a specific contact using ContactsContract.PhoneLookup.CONTENT_FILTER_URI .您可以使用ContactsContract.PhoneLookup.CONTENT_FILTER_URI将短信中的电话号码与特定联系人链接起来。 More detailed information about contacts are in sub-uris of ContactsContract ContentProvider.有关联系人的更多详细信息位于ContactsContract ContentProvider 的子 uris 中。 Description about all of them you will find in docs .您可以在文档中找到关于所有这些的描述。
Below is simple example.下面是简单的例子。 Contact photos isn't stored like photos, but only like a uri to the photo, so you must fetch an image via uri.联系人照片不像照片那样存储,而只是像照片的 uri,因此您必须通过 uri 获取图像。 I didn't test that code too long, so I can't tell you, how it will work eg when you have a few contacts with same number.我没有测试该代码太久,所以我不能告诉你它是如何工作的,例如当你有几个相同号码的联系人时。

if (cursor.moveToFirst()) {
        while (cursor.moveToNext()) {
            String number = cursor.getString(cursor.getColumnIndexOrThrow("address"));
            String thread_id = cursor.getString(cursor.getColumnIndexOrThrow("thread_id"));
            String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
            String name = "";
            String photoUri = "";

            if (number != null) {
                Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number);
                String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.PHOTO_URI};
                Cursor contactCursor = getContentResolver().query(uri, projection, null, null, null);
                if (contactCursor != null && contactCursor.getCount() > 0) {
                    if (contactCursor.moveToFirst()) {
                        do {
                            name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                            photoUri = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
                        } while (contactCursor.moveToNext());
                    }
                    contactCursor.close();
                }
            }
        }
    }
    cursor.close();

Note that you need gain Manifest.permission.READ_CONTACTS permission to read informations about contacts.请注意,您需要获得Manifest.permission.READ_CONTACTS权限才能读取有关联系人的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM