Android Get Contact Picture from Call Log

后端 未结 5 1230
南笙
南笙 2020-12-18 13:07

It was pretty easy to get the Contact picture when querying the People.CONTENT_URI, with a simple

People.loadContactPhoto(activity, ContentUr         


        
5条回答
  •  一向
    一向 (楼主)
    2020-12-18 13:56

    Try this ...

    public Bitmap getPhoto(String phoneNumber) {
        Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        Uri photoUri = null;
        ContentResolver cr = getContentResolver();
        Cursor contact = cr.query(phoneUri,
                new String[] { ContactsContract.Contacts._ID }, null, null, null);
    
        if (contact.moveToFirst()) {
            long userId = contact.getLong(contact.getColumnIndex(ContactsContract.Contacts._ID));
            photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, userId);
    
        }
        else {
            Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture);
            return getCircleBitmap(defaultPhoto);
        }
        if (photoUri != null) {
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
                    cr, photoUri);
            if (input != null) {
                return getCircleBitmap(BitmapFactory.decodeStream(input));
            }
        } else {
            Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture);
            return getCircleBitmap(defaultPhoto);
        }
        Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture);
        contact.close();
        return defaultPhoto;
    }
    

提交回复
热议问题