根據您的使用情況下,有可能是另一個(更好?)選項。如果你有一個所有聯繫人的光標,並且你說,將它們綁定到一個列表視圖,你可以簡單地嘗試使用openContactPhotoInputStream檢索一張照片,並檢查是否爲空。
InputStream stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), lookupUri);
if (stream == null) {
//your logic here when no photo found
}
這裏的任何一個簡單的例子誰可以在此絆倒:
import android.provider.ContactsContract.Contacts;
//Define someDefaultPhoto somewhere accessible
protected Bitmap getContactPhoto(Context context, Uri lookupUri) {
Bitmap resultPhoto;
InputStream stream;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//For some reason this seems to be necessary pre-ICS. If anyone knows why, I'd love to hear
lookupUri = Contacts.lookupContact(context.getContentResolver(), lookupUri);
}
stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), lookupUri);
if (stream == null) {
resultPhoto = someDefaultPhoto;
}
else {
resultPhoto = BitmapFactory.decodeStream(stream);
}
return resultPhoto;
}
然後你可以用這樣的稱呼它:
int columnLookupKey = cursor.getColumnIndex(Contacts.LOOKUP_KEY);
String lookupKey = cursor.getString(columnLookupKey);
Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey);
Bitmap contactPhoto = getContactPhoto(context, lookupUri);
你是以eclair還是cupcake/donut爲目標? – Schildmeijer 2010-03-19 20:20:24
android sdk 2.1 – Pentium10 2010-03-19 20:21:48