Warm tip: This article is reproduced from serverfault.com, please click

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

(java - How to Fetch and Display Name and Image of Conversation's Contact?)

发布于 2021-01-13 18:56:29

我想在下面的屏幕截图中实现类似的功能,到目前为止,我能够显示带有消息计数的最新消息列表,但是当我尝试通过“地址”获取人名时,出现错误“未找到列地址”。 请帮我显示“带有他们图像的联系人姓名”。

我想要达到的目标

    @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;
     }
Questioner
Utaiba Naqshbandi
Viewed
0
grabarz121 2021-01-14 20:01:46

对话 uri 不包含像“地址”这样的列。你可以通过循环简单地查看特定 uri 中的所有列cursor.getColumnName(index)并将其打印到 logcat。你还可以在docs 中找到特定 uri 中的列列表
基本上,Sms ContentProvider 不包含有关联系人的信息,因此你需要对ContactsContracturi进行第二次查询,并通过他的电话号码找到特定联系人。根据你需要的数据,有许多 uri。你可以简单地使用 将短信中的电话号码与特定联系人联系起来ContactsContract.PhoneLookup.CONTENT_FILTER_URI有关联系人的更多详细信息位于ContactsContractContentProvider 的子 uri 中你将在docs 中找到有关所有这些的说明
下面是简单的例子。联系人照片不像照片那样存储,而只是像照片的 uri,因此你必须通过 uri 获取图像。我没有对该代码进行过长时间的测试,所以我无法告诉你它是如何工作的,例如,当你有几个具有相同号码的联系人时。

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阅读联系人信息的权限。