如何获取和显示对话联系人的姓名和图像? [英] How to Fetch and Display Name and Image of Conversation's Contact?

查看:68
本文介绍了如何获取和显示对话联系人的姓名和图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下屏幕快照中的类似功能,到目前为止,我能够显示带有消息计数的最新消息列表,但是当我尝试通过地址"获取人名时,我我得到错误"未找到列地址".请帮助我显示联系人姓名及其图片".

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

推荐答案

会话uri不包含"address"之类的列.您只需通过循环 cursor.getColumnName(index)来查找特定uri中的所有列,然后将其打印到logcat.您还可以在 docs 中找到特定uri的列列表.
基本上,Sms ContentProvider不包含有关联系人的信息,因此您需要对 ContactsContract uri进行第二次查询,并通过其电话号码查找特定的联系人.根据您需要的数据,有很多URI.您可以简单地使用 ContactsContract.PhoneLookup.CONTENT_FILTER_URI 将短信中的电话号码链接到特定联系人.有关联系人的更多详细信息,请参见 ContactsContract ContentProvider的子uri.您可以在文档中找到所有这些文件的说明.
以下是简单的示例.联系人照片并不像照片那样存储,而只是像照片的uri一样存储,因此您必须通过uri获取图像.我没有对该代码进行太长时间的测试,因此无法告诉您它如何工作,例如当您有几个具有相同号码的联系人时.

Conversations uri doesn't contain a column like "address". You can simply look all columns in specific uri via looping cursor.getColumnName(index) and print it to logcat. List of columns into specific uri you can also find in docs.
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. Depending of data that you need, there are many uris. You can simply link a phone number from sms with a specific contact using ContactsContract.PhoneLookup.CONTENT_FILTER_URI. More detailed information about contacts are in sub-uris of ContactsContract ContentProvider. 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. I didn't test that code too long, so I can't tell you, how it will work e.g. 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();

请注意,您需要获得 Manifest.permission.READ_CONTACTS 权限才能读取有关联系人的信息.

Note that you need gain Manifest.permission.READ_CONTACTS permission to read informations about contacts.

这篇关于如何获取和显示对话联系人的姓名和图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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