从联系人电子邮件中检索联系人姓名时获取空值

Getting null value while retrieving the CONTACT NAME from CONTACT EMAIL

我想从联系人电子邮件中检索联系人姓名,所以我所做的如下 。

 public static String readContacts(Context context, String email) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if (cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }

    if (!cursor.isClosed()) {
        cursor.close();
    }

    Log.e("....contact name....", email + "\n" + contactName);

    return contactName;

}

然后在 onCreate() 中,我编码为,

 sName = readContacts(getApplicationContext(), sEmail);

    etName.setText(sName);

但是我得到的是空值。因此,仅根据联系人电子邮件地址获取联系人姓名的解决方案是什么?

EDIT-1:

我已经在清单中提到了权限,

<uses-permission android:name="android.permission.READ_CONTACTS"/>

EDIT-2:

根据ROHIT SHARMA的回答,我更改了如下代码。

 public static String readContacts(Context context, String email) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;

    if (cursor.getCount() > 0) {
        cursor.moveToFirst();

    } else {
        return null;
    }

    if (cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }

    if (!cursor.isClosed()) {
        cursor.close();
    }

    Log.e("....contact name....", email + "\n" + contactName);

    return contactName;

}

但这对我也没有帮助。

EDIT-3:

public static String readContacts(Context context, String email) {
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
String contactName = null;
if(cursor!=null && cursor.getCount()>0 )
{ 
 cursor.moveToFirst();
 contactName =  cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}else{ 
   return null; 
} 
if (!cursor.isClosed()) {
    cursor.close();
} 

Log.e("....contact name....", email + "\n" + contactName);

return contactName;

}

它对我也没有帮助。

EDIT-4:

我试过了

 public String readContacts(Context context, String email) {

    String name = null;

    // define the columns I want the query to return
    String[] projection = new String[]{
            ContactsContract.PhoneLookup.DISPLAY_NAME,
            ContactsContract.PhoneLookup._ID};

    // encode the email and build the filter URI
    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));

    // query time
    Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
            Log.e("....email.....", "Started uploadcontactphoto: Contact Found @ " + email);
            Log.e("....name....", "Started uploadcontactphoto: Contact name  = " + name);
        } else {
            Log.e("....email exception....", "Contact Not Found @ " + email);
        }
        cursor.close();
    }
    return name;

}

来自 但也没有帮助。

有人有其他出路吗?

EDIT-5:

public String readContacts() {

    ContentResolver cr = getContentResolver();

    @SuppressLint("Recycle")

    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

    String id, name = null, email = null;

    if (cur != null && cur.getCount() > 0) {
        while (cur.moveToNext()) {
            id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                System.out.println("name : " + name + ", ID : " + id);

                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                if (pCur != null) {
                    while (pCur.moveToNext()) {
                        String phone = pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        System.out.println("phone" + phone);
                    }
                }
                if (pCur != null) {
                    pCur.close();
                }


                // get email and type

                Cursor emailCur = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                if (emailCur != null) {
                    while (emailCur.moveToNext()) {
                        // This would allow you get several email addresses
                        // if the email addresses were stored in an array
                        email = emailCur.getString(
                                emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        String emailType = emailCur.getString(
                                emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));


                        System.out.println("Email " + email + " Email Type : " + emailType);
                    }

                    if (email == sEmail) {
                        sName = name;
                    }

                }
                if (emailCur != null) {
                    emailCur.close();
                }
            }
        }
    }
    return name;
}

我尝试了来自 http://www.coderzheaven.com/2011/06/13/get-all-details-from-contacts-in-android/ 的上述代码实例,我得到了名称,但它与电子邮件 ID 所有者的名称不同。

所以,告诉我哪里错了..

从上面的代码片段,我也试过了

if (email == sEmail) {
                            sName = name;
                        }
                        System.out.println("Email " + email + " Email Type : " + emailType);
                    }



                }
                if (emailCur != null) {
                    emailCur.close();
                }

而不是

                        System.out.println("Email " + email + " Email Type : " + emailType);
                    }

                    if (email == sEmail) {
                        sName = name;
                    }

                }
                if (emailCur != null) {
                    emailCur.close();
                }

但也没有帮助。

使用前务必检查 getCount。

if(cursor!=null && cursor.getCount()>0 )
{
 cursor.moveToFirst();


}else{
   return null;
}

同时检查您是否已在清单中声明读取联系人的权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />

您还需要其他一些相关权限,请查看文档了解具体内容。

我建议尝试这种方式并使用断点调试您的代码 哪里有问题。

public static String readContacts(Context context, String email) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    String contactName = null;
    if(cursor!=null && cursor.getCount()>0 )
    {
     cursor.moveToFirst();
     contactName =  cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }else{
       return null;
    }
    if (!cursor.isClosed()) {
        cursor.close();
    }

    Log.e("....contact name....", email + "\n" + contactName);

    return contactName;

}

你应该更换

 Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);

Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI, Uri.encode(email));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.CommonDataKinds.Email.CONTACT_ID,
            ContactsContract.Data.DISPLAY_NAME}, null, null, null);

然后测试一下..