我有一个自定义来电屏幕,只要收到来电,就会显示。我已经能够从我的联系人中捕获呼叫者的姓名和号码,并将其分配给我自己的文本视图,但获取联系人照片ID已被证明是一个巨大的痛苦。以下是根据电话号码处理联系人照片的代码:
int idCol = cur.getColumnIndex(ContactsContract.Contacts._ID);
long contactPhoto = Long.parseLong(IncomingCallListener.getPhoneNumberSt8());
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactPhoto);
Bitmap bitmap = getDisplayPhoto(contactPhoto);
qcbContactPic.setImageBitmap(bitmap);
cur 是一个游标。
contactPhoto 获取传入电话号码的字符串,然后将其解析为长。
IncomingCallListener 是我的BroadcastReceiver类。
qcbContactPic 是QuickContactBadge。
这个编译没有错误,但有人知道为什么我收到来电时照片不会显示在QuickContactBadge中吗?
注意:我不是想尝试使用facebook pics。我正在使用从设备本身拍摄的手机图库中存储的照片。
更新更新更新
这是新代码。这是为了允许检索调用者的联系人照片,但它仍然返回我设置的默认图像:
public Uri getPhotoUri() {
try {
Cursor cur = context.getContentResolver().query( //this.
ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID + "=" + this.getID() + " AND "
+ ContactsContract.Data.MIMETYPE + "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
null);
if (cur != null) {
if (!cur.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
.parseLong(IncomingCallListener.getPhoneNumberSt8()));
return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
public String getID(){
return IncomingCallListener.getPhoneNumberSt8();
}
然后就像这样调用:
Uri u = getPhotoUri();
if (u != null) {
qcbContactPic.setImageURI(u);
Log.d("PHOTO", "ID launched");
} else {
qcbContactPic.setImageResource(R.drawable.ic_launcher);
Log.d("PHOTO", "Default launched");
}
注意: IncomingCallListener.getPhoneNumberSt8()返回电话号码的字符串。我已经使用联系人照片设置了电话号码5555551234,但是当我从telnet拨打电话到模拟器时,显示“默认启动”而不是“ID已启动”,并显示相应的图片。
LOGCAT (除最后一个条目以外的所有警告,显示使用了哪张照片):
04-29 05:45:31.581: W/System.err(16332): java.lang.NullPointerException
04-29 05:45:31.590: W/System.err(16332): at com.fooapp.barname.IncomingCallReceived.getPhotoUri(IncomingCallReceived.java:239)
04-29 05:45:31.590: W/System.err(16332): at com.fooapp.barname.IncomingCallReceived.getContactName(IncomingCallReceived.java:225)
04-29 05:45:31.590: W/System.err(16332): at com.fooapp.barname.IncomingCallReceived.onCreate(IncomingCallReceived.java:99)
04-29 05:45:31.590: W/System.err(16332): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-29 05:45:31.590: W/System.err(16332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-29 05:45:31.590: W/System.err(16332): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-29 05:45:31.590: W/System.err(16332): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-29 05:45:31.590: W/System.err(16332): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-29 05:45:31.590: W/System.err(16332): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 05:45:31.600: W/System.err(16332): at android.os.Looper.loop(Looper.java:123)
04-29 05:45:31.600: W/System.err(16332): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-29 05:45:31.600: W/System.err(16332): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 05:45:31.600: W/System.err(16332): at java.lang.reflect.Method.invoke(Method.java:507)
04-29 05:45:31.600: W/System.err(16332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-29 05:45:31.600: W/System.err(16332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-29 05:45:31.600: W/System.err(16332): at dalvik.system.NativeStart.main(Native Method)
04-29 05:45:31.620: D/PHOTO(16332): Default launched
答案 0 :(得分:0)
这是一个有效的例子
public class QuickContactBadgeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
QuickContactBadge badge = (QuickContactBadge) findViewById(R.id.small_contact_badge);
String contactId = fetchContactIdFromPhoneNumber("123");
Uri uri = getPhotoUri(Long.parseLong(contactId));
badge.assignContactUri(uri);
badge.setImageBitmap(loadContactPhoto(getContentResolver(), Long.parseLong(contactId)));
}
private String fetchContactIdFromPhoneNumber(String phoneNumber) {
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cursor = this.getContentResolver().query(uri,
new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
null, null, null);
String contactId = "";
if (cursor.moveToFirst()) {
do {
contactId = cursor.getString(cursor
.getColumnIndex(PhoneLookup._ID));
} while (cursor.moveToNext());
}
return contactId;
}
private static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
if (input == null) {
return null;
}
return BitmapFactory.decodeStream(input);
}
private Uri getPhotoUri(long contactId) {
ContentResolver contentResolver = getContentResolver();
try {
Cursor cursor = contentResolver
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ contactId
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cursor != null) {
if (!cursor.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri person = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactId);
return Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
}
它基于的例子:
How do I load a contact Photo?
您还需要知情权:
<uses-permission android:name="android.permission.READ_CONTACTS"/>