如何将联系人照片添加到谷歌地图的MARKER?

时间:2015-06-02 11:35:31

标签: android google-maps

我想将MARKER图标添加为联系人的照片

我正在使用此代码来检索联系人照片

public static Bitmap getContactPhoto(String phoneNumber) {
        ContentResolver cr = Singleton.getInstance().getContentResolver();
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        Cursor cursor = cr.query(uri, new String[]{PhoneLookup.PHOTO_URI}, null, null, null);
        if (cursor == null) {
            return null;
        }
        byte[] data= {99, 111, 110, 116, 101, 110, 116, 58, 47, 47, 99, 111, 109, 46, 97, 110, 100, 114, 111, 105, 100, 46, 99, 111, 110, 116, 97, 99, 116, 115, 47, 100, 105, 115, 112, 108, 97, 121, 95, 112, 104, 111, 116, 111, 47, 49, 0};

        if(cursor.moveToFirst()) {
            data = cursor.getBlob(cursor.getColumnIndex(PhoneLookup.PHOTO_URI));
            if (data != null) {
                return BitmapFactory.decodeByteArray(data, 0, data.length());
            }
            else
                return BitmapFactory.decodeByteArray(data, 0, data.length());

        }

        if(cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return BitmapFactory.decodeByteArray(data, 0, data.length());
}

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
        if (input == null) {
            return null;
        }
        return BitmapFactory.decodeStream(input);

和 添加标记和图标

BitmapDescriptor bitmap = BitmapDescriptorFactory.fromBitmap(getContactPhoto("9888888888"));
            myLocationMarker =  googleMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(latLng).title("My Location").icon(bitmap));

但它不起作用

1 个答案:

答案 0 :(得分:1)

最后我想出来,你需要两种方法:

首先,您需要从手机号码获取contactId

public static long getContactIDFromNumber(String contactNumber, Context context) {
    String UriContactNumber = Uri.encode(contactNumber);
    long phoneContactID = new Random().nextInt();
    Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, UriContactNumber),
            new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);
    while (contactLookupCursor.moveToNext()) {
        phoneContactID = contactLookupCursor.getLong(contactLookupCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
    }
    contactLookupCursor.close();

    return phoneContactID;
}

其次,从Bitmap获取contactId

public Bitmap openPhoto(long contactId) {
        Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
        Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
        Cursor cursor = getContentResolver().query(photoUri,
                new String[]{ContactsContract.Contacts.Photo.PHOTO}, null, null, null);
        if (cursor == null) {
            return null;
        }
        try {
            if (cursor.moveToFirst()) {
                byte[] data = cursor.getBlob(0);
                if (data != null) {
                    return BitmapFactory.decodeStream(new ByteArrayInputStream(data));
                }
            }
        } finally {
            cursor.close();
        }
        return null;
    }

有关此方法的详细信息,请参阅here

最后,设置地图图标的位图:

BitmapDescriptor bitmap = BitmapDescriptorFactory.fromBitmap(openPhoto(getContactIDFromNumber("PHONE NUMBER", this)));

    Marker marker = mGoogleMap.addMarker(new MarkerOptions()
            .position(new LatLng(Double.parseDouble(latLng)))
            .title("Have a nice day!")
            .icon(bitmap));

请注意:

  • 别忘了更改电话号码。
  • 不要忘记添加权限:android.permission.READ_CONTACTSandroid.permission.WRITE_CONTACTS

有关完整源代码,请参阅here