I have an custom incoming call screen that shows up whenever an incoming call is received. I've been able to capture the name and number of the caller from my contacts and assign them to my own textviews, but getting the contact photo ids have proven to be a great pain. Here is the code that is suppose to handle getting the contact's photo based on the the phone number:
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 is a cursor.
contactPhoto takes the string of the incoming phone number, then parses it to long.
IncomingCallListener is my class for the BroadcastReceiver.
qcbContactPic is the QuickContactBadge.
This compiles without error, but does anyone know why the photo will not show in the QuickContactBadge when I receive an incoming call?
NOTE: I'm not trying to use facebook pics. I'm using photos stored from the phone's gallery taken from the device itself.
UPDATE UPDATE UPDATE
Here is the new code. This is suppose to allow retrieve the contact photo of the caller, but it still returns the default image I've set:
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();
}
Then it is called like this:
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");
}
NOTE: IncomingCallListener.getPhoneNumberSt8() returns the String of the phone number. I've already set the phone number 5555551234 with a contact photo, but when I make the call from telnet to the emulator, the "Default launched" is shown instead of "ID launched" with the appropriate picture.
LOGCAT (all warnings except for last entry showing which photo is used):
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