我想推荐几件事以及您可以尝试的解决方案,如果它仍然失败,请告诉我。
1)您正在为您的应用程序使用 SMS 自定义提供程序,如果用户将他的操作系统更新到 4.0 或更高版本,就像我尝试他们在其中工作时一样,这可能会失败。
2) 如果您尝试查询不在 getView() 中的所有自定义提供程序会更好,因为它会加快进程和列表视图。
3)我觉得你的代码的问题是当你试图获取联系人图像时,这有点典型,因为我之前实现过,结果不像博客中提到的那样,不知道为什么。
4)现在解决方案流程:
- 您有要查询的电话号码。
- 现在我们将获取该号码并查询电话簿内容提供者的姓名。
- 如果名称不为空,我们可能会继续获取特定的号码 ID(检查您是否可以直接获取 ID 并在不获取名称的情况下进行操作)
- 如果contactId 不为null,则获取图片并将其放在您的ListView 中。
不要忘记放置空检查并按照提到的代码进行操作,如果失败请告诉我。
5) 守则:
获取联系人姓名:
/**
* Method used to fetch CONTACT NAME FOR CONTACT NUMBER
*
* @param number :: Contact Number
* @param context :: Activity context
* @return :: returns CONTACT-NAME for CONTACT NUMBER
*/
private String getContactNameFromNumber(String number, Context context)
{
String contactName = null;
Uri contactUri = null;
String[] projection = null;
String selection = null;
Cursor cursorContactId = null;
/*
* Defining URI, Projection and Selection elements
*/
contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
projection = new String[]{Contacts._ID,Contacts.DISPLAY_NAME};
selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + number;
/*
* Cursor iterator to fetch
* particular ID
*/
try
{
cursorContactId = context.getContentResolver().query(contactUri, projection, selection, null, null);
while (cursorContactId.moveToNext())
{
contactName = cursorContactId.getString(cursorContactId.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
cursorContactId.close();
}
if(contactName != null)
{
return contactName;
}
else
{
return null;
}
}
获取联系人 ID(如果 name 不为null):
/**
* Fetching Contact from a Number
* @param number - For which specific Id is required
* @param context - current context
* @return - ID as String
*/
private String getContactIdFromNumber(String number, Context context)
{
String contactId = null;
Uri contactUri = null;
String[] projection = null;
String selection = null;
Cursor cursorContactId = null;
/*
* Defining URI, Projection and Selection elements
*/
contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
projection = new String[]{Contacts._ID};
selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + number;
/*
* Cursor iterator to fetch
* particular ID
*/
try
{
cursorContactId = context.getContentResolver().query(contactUri, projection, selection, null, null);
while (cursorContactId.moveToNext())
{
contactId = cursorContactId.getString(cursorContactId.getColumnIndex(ContactsContract.Contacts._ID));
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
cursorContactId.close();
}
if(contactId != null)
{
return contactId;
}
else
{
return null;
}
}
最后加载联系人图片(如果 ID 不为空。传递 id 和 photo_id 相同的 ID 值。此代码来自搜索各种链接后的 SO):
/**
* Fetching contact picture from PhoneBook if available
* @param contentResolver - Current Content Resolver context
* @param id - Specific Contact Id as retrieved
* @param photo_id - Same as Contact Id
* @return - Contact Byte Array if present or null
*/
private byte[] loadContactPhoto(ContentResolver contentResolver, String id, String photo_id)
{
byte[] result = null;
byte[] photoBytes = null;
/*
* Step 1 : Directly fetching with contactId or execute Step 2
*/
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri);
/*
* Execute only if stream is not null
*/
if (input != null)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
{
int next = input.read();
while (next > -1)
{
bos.write(next);
next = input.read();
}
bos.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
result = bos.toByteArray();
return result;
// return BitmapFactory.decodeStream(input); // Open if want to return as Bitmap
}
else
{
Log.d("PHOTO","first try failed to load photo");
}
/*
* Step 2: Image not fetched directly, fetching through traversing
*/
Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, Long.parseLong(photo_id));
Cursor cursorImageFetch = contentResolver.query(photoUri, new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null);
try
{
if (cursorImageFetch.moveToFirst())
photoBytes = cursorImageFetch.getBlob(0);
}
catch (Exception e)
{
e.printStackTrace();
} finally {
cursorImageFetch.close();
}
if (photoBytes != null)
{
return photoBytes;
//return BitmapFactory.decodeByteArray(photoBytes,0,photoBytes.length); // Open if want to return as Bitmap
}
else
{
Log.d("PHOTO","Picture of the contact not available");
return getByteArray();
}
}