1

I have create custom incoming call screen that can current show the name and phone number of an incoming call. If the contact has a profile pic set that image should display in the ImageView. I calls the below method, and assigns the picture to the ImageView. The problem is that the else statement is always executed:

public static Uri getPhotoUri(String savedNumber, Context context) {
    try {
        Cursor cur = context
                .getContentResolver()
                .query(
                        ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID
                                + "="
                                + getContactIDFromNumber(savedNumber,
                                context)
                                + " 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,
            getContactIDFromNumber(savedNumber, context));
    return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

public static int getContactIDFromNumber(String contactNumber,Context context) {
    int phoneContactID = 0;
    Cursor contactLookupCursor = context.getContentResolver().query(
            Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(contactNumber)),
            new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID},
            null, null, null);

    try {
        contactLookupCursor.moveToFirst();
        while (contactLookupCursor.moveToNext()) {
            phoneContactID = contactLookupCursor.getInt(contactLookupCursor
                    .getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
        }
    } finally {
        contactLookupCursor.close();
    }
    return phoneContactID;
}

IncomingActivity:

@Override
protected void onResume() {
    super.onResume();

    callerNumber.setText(savedNumber);
    callerName.setText(CommonMethods.getCallerName(savedNumber, this));

    callerImageUri = CommonMethods.getPhotoUri(savedNumber, this);
    if (callerImageUri != null) {
        callerImage.setImageURI(callerImageUri);
    } else {
        callerImage.setImageResource(R.drawable.contact_default);
    }
}

Does anyone have any ideas as to why "callerInamgeUri" is remaining null?

Secret
  • 611
  • 1
  • 10
  • 29

0 Answers0