0

我有一个自定义来电屏幕,每当收到来电时就会显示。我已经能够从我的联系人中捕获来电者的姓名和号码,并将它们分配给我自己的文本视图,但事实证明,获取联系人照片 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获取传入电话号码的字符串,然后将其解析为 long。

IncomingCallListener是我的 BroadcastReceiver 类。

qcbContactPic是 QuickContactBadge。

这编译没有错误,但是有谁知道为什么当我接到来电时照片不会显示在 QuickContactBadge 中?

注意:我不想使用 facebook 图片。我正在使用从设备本身拍摄的手机图库中存储的照片。

更新更新更新

这是新代码。这假设允许检索来电者的联系人照片,但它仍然返回我设置的默认图像:

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
4

1 回答 1

1

这是一个工作示例

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

}

它基于的示例:

如何加载联系人照片?

如何获取联系人照片 URI

您还需要许可:

<uses-permission android:name="android.permission.READ_CONTACTS"/>
于 2012-04-28T10:36:52.977 回答