連絡人頭像android

來源:互聯網
上載者: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()) { 
//獲得連絡人的id           
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
 
//修改連絡人的頭像 
Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img2); 
final ByteArrayOutputStream os = new ByteArrayOutputStream(); 
// 將Bitmap壓縮成PNG編碼,品質為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> 
 
構建uri
 

Uri contactUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, 
Long.parseLong(contactId)); 
//帶路徑 
Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DATA15); 
 此uri為content://com.android.contacts/contactId/data15
 
獲得連絡人圖片
 
// 獲得連絡人圖片,     讀取的是Data類中的data15欄位 
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); 
 
 查詢電話記錄
 
// 查詢所有 
// Cursor cursor=resolver.query(CallLog.Calls.CONTENT_URI, null, 
        // null,null, null); 
// 查詢指定號碼 
Cursor cursor=resolver.query(CallLog.Calls.CONTENT_URI, null, 
          "number=? and type=?", new String[]{"15555215556","2"}, null); 
 while(cursor.moveToNext()){ //取得連絡人名字 PhoneLookup.DISPLAY_NAME int nameFieldColumnIndex 
         =cursor.getColumnIndex(CallLog.Calls.CACHED_NAME); String 
    contact=cursor.getString(nameFieldColumnIndex);  
//取得電話號碼 
 int numberFieldColumnIndex=cursor.getColumnIndex(PhoneLookup.NUMBER); 
 String number=cursor.getString(numberFieldColumnIndex); 
 

作者:hahashui123

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.