我想推薦一些東西,也該,你可以嘗試,讓我知道,如果它仍然失敗的解決方案。
1)您使用的短信定製商爲您的應用程序,如果用戶更新自己的OS 4.0或以上,當我試着他們不力在他們的工作可能會失敗。
2)這將是更好,如果你嘗試查詢所有定製的提供者不getView(),因爲它會加快這一進程和ListView。
3)我覺得你的代碼的問題是,當你試圖取回接觸式圖像,正如我前面實施和犯規原來在博客中提到,不知道爲什麼,這是有點典型。
4)現在清水流量:
- 您有您要查詢的電話號碼。
- 現在我們將採用該號碼並查詢電話簿內容提供商的名稱。
- 如果名不爲空我們可能會在獲得一定數量的特定ID進行(檢查是否可以直接讀取ID和做沒有取名字)
- 如果的ContactID不爲空獲取圖片,並把它在你的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(如果名稱不空):
/**
* 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不爲空ss 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();
}
}
您參考鏈接斷開。 –