Empty Cursor when querying contact uri

本小妞迷上赌 提交于 2019-12-12 17:26:34

问题


My little test app picks one contact using the contact picker:

    final Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
            android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, PICK_CONTACT);

in the onActivityResult method, I parse the result and it's something like content://com.android.contacts/data/7557. I then query this uri to get its name and lookup key:

final String[] projection = new String[] {
    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
    ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY };
final Cursor c = getContentResolver().query(contactUri, projection, null, null, null);
if (c.moveToFirst()) {
    final String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    final String lookupKey = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY));
    Log.d(TAG, "picked contact, name: " + name + ", lookup key: " + lookupKey);
}

It works fine, but not for SOME contacts. Some contacts will result in an empty cursor. I checked out other questions. This one seems to have the same problem querying contacts - SOMETIMES returns empty cursor (but no answers). I tried the approach of the solution proposed in this question, Retrieve Contact Phone Number From URI in Android:

final String id = contactUri.getLastPathSegment();
final String[] whereArgs = new String[] { id };
final Cursor c2 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
    ContactsContract.CommonDataKinds.Phone._ID + " = ?", whereArgs, null);
if (c2.moveToFirst()) {
    final String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    final String lookupKey = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY));
}

but unfortunately the problem stays: SOME contacts will result in an empty cursor. What am I doing wrong? Thanks anybody for the help.

来源:https://stackoverflow.com/questions/14111003/empty-cursor-when-querying-contact-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!