我在将ImageView
ListView
中的ListView
设置为联系人图片时遇到问题。我已经尝试了从开发者网站到SO的每个所谓的“解决方案”,但它们似乎都没有用。
这是填充我的listview
的CustomAdapter类:
http://pastebin.com/ABeK9nmT
这是我持有listview
的活动:
http://pastebin.com/FcBDprfG
重要的是要知道该活动包含W/Resources(5997): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
周围有不可移动的对象。
运行代码时的问题是只有默认照片注册而没有显示联系人。
当活动开始时, LogCat 会为6个联系人显示此信息:
Contacts.Contract
我没有收到任何错误。有谁知道我可能做错了什么?
请不要复制和粘贴来自其他SO“解决方案的编码。”我已经尝试了所有这些,并且它们要么是较低的API,要么是从另一个答案中复制以获得积分,或者也是许多数据。我正在使用 2.3.3 ,它使用{{1}}。
答案 0 :(得分:1)
我想推荐一些事情以及您可以尝试的解决方案,如果它仍然失败,请告诉我。
1)您正在为您的应用程序使用SMS自定义提供程序,如果用户将其操作系统更新到4.0或更高版本,那么可能会失败,就像我尝试使用它们一样。
2)如果你尝试查询不在getView()中的所有自定义提供程序会更好,因为它会加快进程和列表视图。
3)我觉得代码的问题在于你试图获取联系人图片,这在我之前实现的时候有点典型,并且没有像博客中提到的那样,不知道原因。
4)现在解决方案流程:
不要忘记进行空检查并按照上述代码进行操作,如果失败请告诉我。
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(如果名称不是 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();
}
}