0

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
tshepang
  • 12,111
  • 21
  • 91
  • 136

1 Answers1

1

Here's a working example

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);
}

}

The example it's based on :

How do I load a contact Photo?

how to get contact photo URI

You will also need the permision :

<uses-permission android:name="android.permission.READ_CONTACTS"/>
Community
  • 1
  • 1
dsafcdsge4rfdse
  • 260
  • 2
  • 12
  • setting qcbContactPic to drawable brings an error. Can you show me what you mean? Plus, I can't hardcode an image just for the fact that a person might later on change the contact picture. I just need to be able to assign to the quickcontactbadge for my screen whatever the photo is attached to the contact. –  Apr 28 '12 at 10:47
  • No, it didn't help. It caused my app to crash when a call was made from telnet. –  Apr 28 '12 at 23:07
  • could you provide the output of Logcat? – dsafcdsge4rfdse Apr 29 '12 at 05:46
  • the line 239 from your `IncomingCallReceived.java` is `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);` ? – dsafcdsge4rfdse Apr 29 '12 at 06:16
  • to me it looks that your `context` or/and the value returned by `getContentResolver()` are null. can you check that? – dsafcdsge4rfdse Apr 29 '12 at 06:36
  • I see that I never really set context to anything. I just have Context context at the top of my class. How would I set this? –  Apr 29 '12 at 06:43
  • I'm guessing that in order to obtain the incoming call phone's number your using a broadcast receiver, you can obtain a context from the method `onReceive (Context context, Intent intent)` which is called by the system when a incoming call is received. – dsafcdsge4rfdse Apr 29 '12 at 06:48
  • So I should use the context from my broadcast receiver then –  Apr 29 '12 at 07:04
  • Cant use the context from the onReceive method, because it is only defined within the method –  Apr 29 '12 at 07:21