我想从联系电子邮件中检索联系人姓名,因此我所做的工作如下https://stackoverflow.com/a/18064869/5738881。
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;
}
来自https://stackoverflow.com/a/15007980/5738881的但也没有帮助。
有人有其他出路吗?
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();
}
但也没有帮助。
答案 0 :(得分:0)
使用前请务必检查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;
}
答案 1 :(得分:0)
你应该替换
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);
然后测试它..