Android get contact Uri

public Uri getContactUri(String contactId, Context context)  {

        if (context == null || contactId == null) {
            return null;
        }

        // Get the Uri of contact information 
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
         // Get ContentResolver 
        ContentResolver contentResolver = context.getContentResolver();
         // Query data, return Cursor 
        Cursor cursor = contentResolver.query(uri, null , null , null , null );


        while (cursor.moveToNext()) {

            // Get the contact's ID 
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
             // Get the contact's name 
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME) );

            String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

            if (id != null && id.equals(contactId)) {

                return ContactsContract.Contacts.getLookupUri(Integer.valueOf(contactId), lookupKey);

            }
        }
        return null;
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325010322&siteId=291194637