简体   繁体   中英

Cannot load contact photo into QuickContactBadge for custom incoming call screen

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

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM