Contact Profile android

Source: Internet
Author: User


<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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.