【问题标题】:Query contacts Thumbnail from contact number从联系人号码中查询联系人缩略图
【发布时间】:2012-11-04 11:06:56
【问题描述】:

我正在尝试从给定电话号码或电子邮件 ID 的电话簿中检索联系人缩略图。我写的代码可以工作,但需要很长时间,因为它正在查询整个联系人(根本没有效率)。

下面是代码

Cursor cursor;
String[] queryColumns = { ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,ContactsContract.Contacts._ID };
ContentResolver cr = getContentResolver();
cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, queryColumns,
                            null, null, null);

cursor.moveToFirst();
while (cursor.moveToNext() && continueSearch) {
    String[] phonesAndEmails = extractPhonesAndEmails(Integer.parseInt(cursor.getString(1)));
    for(int g=0;g<phonesAndEmails.length;g++){
        if(phonesAndEmails[g].equals(searchFor)){
            contactThumbUri = cursor.getString(0);
            MyUtils.addLog("Found Match **************" + contactThumbUri);
            continueSearch=false;
        }
    }
}
cursor.close();

extractPhonesAndEmails 获得一个 String[],其中包含传递给它的 contactID 的所有号码和电子邮件。

我从 SO 那里得到了另一种使用 PhoneLookup 的方法,但是给我一个错误。我检查一下这个游标返回了哪些列。我只有两列,其中一列是联系人姓名,另一列是整数,可能是 ID。这是第二个代码;

if(!MyUtils.checkIfEmailID(searchFor)){
    Cursor mCursor;
    Uri qUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(searchFor));
    String[] mqueryColumns = {PhoneLookup.PHOTO_URI};
    mCursor = getContentResolver().query(qUri, mqueryColumns,null, null, null);
    MyUtils.addLog(mCursor.getCount() + " count");
    MyUtils.addLog(mCursor.getString(0));
    mCursor.moveToFirst();
    while (mCursor.moveToNext() && continueSearch) {
        contactThumbUri=mCursor.getString(0);
        MyUtils.addLog(contactThumbUri);
        continueSearch=false;
    }
    mCursor.close();    
                    }

请帮助优化我的第一个代码或解决第二个代码中的错误。非常感谢。

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    在乔的回答和this post的帮助下, 我能够想出对我来说完美的代码。此代码基本上将电话号码作为输入,并将缩略图 uri 返回给联系人(如果存在于电话中)。这是代码。

    谢谢乔。

     Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(searchFor));
    String[] projection = {PhoneLookup.LOOKUP_KEY};
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    cursor.moveToFirst();
    do{
        String lookUpKey = cursor.getString(0);
    
        uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, Uri.encode(lookUpKey));
        String[] projection1 = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
        Cursor cursor1 = getContentResolver().query(uri, projection1, null, null, null);
        cursor1.moveToFirst();
        contactThumbUri = cursor1.getString(0);
        cursor1.close();
        if(!contactThumbUri.equals(null)){
            continueSearch=false;
        }
    }while(cursor.moveToNext() && continueSearch);
    cursor.close();
    

    类似的方法可用于查找电子邮件。需要使用 ContactsContract.Contacts.CONTENT_LOOKUP_URI 而不是 PhoneLookup.CONTENT_FILTER_URI

    希望这也可以帮助像我这样的其他初学者:P

    【讨论】:

      【解决方案2】:

      首先在 ADDRESS 上查询 ContactsContract.CommonDataKinds.Email 并返回一个包含 ContactsContract.ContactsColumns.LOOKUP_KEY 的投影。这将为您提供具有该电子邮件地址的所有联系人的聚合联系人 ID(不变)。从那里,查询 ContactsContract.Contacts 在 LOOKUP_KEY 上获取照片。或者,获取包含 LOOKUP_KEY 的行的 _ID 值,然后在 ContactsContract.RawContacts.CONTACT_ID 中查询以获取所有原始联系人,然后查询他们的照片。

      联系人数据库是分层的,您必须了解它的分层结构才能有效地使用它。

      此外,您应该始终在后台线程上进行查询。

      【讨论】:

        【解决方案3】:

        试试这个。

            final String[] queryColumns = { ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
                    ContactsContract.Contacts._ID };
        
            final String email = "abc@gmail.com";
        
            final StringBuffer sb = new StringBuffer();
        
            sb.append("mimetype_id = (select _id from mimetypes where mimetype = " +
                    "'vnd.android.cursor.item/email_v2') and data1 = '"+email +"'");
        
            final Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                    queryColumns, sb.toString(), null, null);
        
        
            if (cursor == null || !cursor.moveToFirst()) {
                return;
            }
        
            try {
                do {
                    final String photoURI = cursor.getString(0);
                    final long contactID = cursor.getLong(1);
        
                    Log.i("Test", "PhotoURI"+photoURI+"ContactID"+contactID);
        
                } while (cursor.moveToNext());
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        

        【讨论】:

        • 抱歉 Mittal saab,我无法尝试您的解决方案。不过感谢您的帮助。
        猜你喜欢
        • 1970-01-01
        • 2017-06-22
        • 1970-01-01
        • 2014-03-30
        • 2011-03-09
        • 1970-01-01
        • 2021-07-06
        • 2018-04-10
        • 1970-01-01
        相关资源
        最近更新 更多