<Pre class = "java" name = "code"> ContentResolver cr = getContentResolver ();
Cursor cursor = cr. query (ContactsContract. Contacts. CONTENT_URI, null, "DISPLAY_NAME = '" + "gm" + "'", null,
Null );
If (cursor. moveToFirst ()){
// Obtain the contact id
String contactId = cursor. getString (cursor. getColumnIndex (ContactsContract. Contacts. _ ID ));
// Modify the profile picture of a contact
Bitmap sourceBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. img2 );
Final ByteArrayOutputStream OS = new ByteArrayOutputStream ();
// Compress Bitmap into PNG and store the Bitmap with a quality of 100%
SourceBitmap. compress (Bitmap. CompressFormat. PNG, 100, OS );
Byte [] avatar = OS. toByteArray ();
ContentValues values = new ContentValues ();
Values. put (Data. RAW_CONTACT_ID, contactId );
Values. put (Data. MIMETYPE, Photo. CONTENT_ITEM_TYPE );
Values. put (Photo. PHOTO, avatar );
GetContentResolver (). update (Data. CONTENT_URI, values, null, null); </pre>
Build uri
Uri contactUri = ContentUris. withAppendedId (ContactsContract. Data. CONTENT_URI,
Long. parseLong (contactId ));
// With Path
Uri photoUri = Uri. withAppendedPath (contactUri, ContactsContract. Contacts. Photo. DATA15 );
This uri is content: // com. android. contacts/contactId/data15
Get Contact Image
// Obtain the Contact Image and read the data15 field in the Data class
Uri uri = ContentUris. withAppendedId (ContactsContract. Contacts. CONTENT_URI,
Long. parseLong (contactId); InputStream input =
ContactsContract. Contacts. openContactPhotoInputStream (cr, uri );
Bitmap contactPhoto = BitmapFactory. decodeStream (input );
ImageView img = (ImageView) findViewById (R. id. img );
Img. setImageBitmap (contactPhoto );
Query phone records
// Query all
// Cursor cursor = resolver. query (CallLog. CILS. CONTENT_URI, null,
// Null, null, null );
// Query the specified number
Cursor cursor = resolver. query (CallLog. CILS. CONTENT_URI, null,
"Number =? And type =? ", New String [] {" 15555215556 "," 2 "}, null );
While (cursor. moveToNext () {// obtain the contact name PhoneLookup. DISPLAY_NAME int nameFieldColumnIndex
= Cursor. getColumnIndex (CallLog. CILS. CACHED_NAME); String
Contact = cursor. getString (nameFieldColumnIndex );
// Obtain the phone number
Int numberFieldColumnIndex = cursor. getColumnIndex (PhoneLookup. NUMBER );
String number = cursor. getString (numberFieldColumnIndex );
Author: hahashui123