我正在嘗試從電話簿中獲取電話號碼或電子郵件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();
}
請幫助優化我的第一個代碼或在第二個代碼中排除錯誤。謝謝很多。
對不起米塔爾薩博,我不會能夠嘗試你的解決方案。感謝您的幫助。 – nightcrawler23