Android獲取聯(lián)系人頭像的方法
本文實(shí)例講述了Android獲取聯(lián)系人頭像的方法。分享給大家供大家參考,具體如下:
public byte[] getPhoto(String people_id) { String photo_id = null; String selection1 = ContactsContract.Contacts._ID + " = " + people_id; Cursor cur1 = getContentResolver().query( ContactsContract.Contacts.CONTENT_URI, null, selection1, null, null); if (cur1.getCount() > 0) { cur1.moveToFirst(); photo_id = cur1.getString(cur1.getColumnIndex(ContactsContract.Contacts.PHOTO_ID)); //System.out.println("photo_id:" + photo_id); } String[] projection = new String[] { ContactsContract.Data.DATA15 }; String selection = ContactsContract.Data._ID + " = " + photo_id; Cursor cur = getContentResolver().query( ContactsContract.Data.CONTENT_URI, projection, selection, null, null); cur.moveToFirst(); byte[] contactIcon = cur.getBlob(0); System.out.println("conTactIcon:" + contactIcon); if (contactIcon == null) { return null; } else { return contactIcon; } }
以下代碼將字節(jié)數(shù)組轉(zhuǎn)化成Bitmap對(duì)象,然后再ImageView中顯示出來
private ImageView image; byte[] photo = getPhoto(contactId); Bitmap map = BitmapFactory.decodeByteArray(photo, 0,photo.length); image.setImageBitmap(map);
通過代碼設(shè)置Android聯(lián)系人的頭像:
private final static boolean OldSDK = (System.getSDKVersionNumber()< 5 )? true : false; public static void setPersonPhotoBytes(Context context,byte[] b, long persionID, boolean Sync) { if (OldSDK){ Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, persionID); People.setPhotoData(context.getContentResolver(), myPerson, b); if (! Sync){ ContentValues values = new ContentValues(); values.put("_sync_dirty", 0); context.getContentResolver().update(myPerson, values, null, null); } } else setContactPhoto5(context.getContentResolver(), b, persionID, Sync); } private static void setContactPhoto5(ContentResolver c, byte[] bytes,long personId, boolean Sync) { ContentValues values = new ContentValues(); Uri u = Uri.parse("content://com.android.contacts/data"); int photoRow = -1; String where ="raw_contact_id = " + personId + " AND mimetype ='vnd.android.cursor.item/photo'"; Cursor cursor = c.query(u, null, where, null, null); int idIdx = cursor.getColumnIndexOrThrow("_id"); if (cursor.moveToFirst()) { photoRow = cursor.getInt(idIdx); } cursor.close(); values.put("raw_contact_id", personId); values.put("is_super_primary", 1); values.put("data15", bytes); values.put("mimetype","vnd.android.cursor.item/photo"); if (photoRow >= 0) { c.update(u, values, " _id= " + photoRow, null); } else { c.insert(u, values); } if (! Sync){ u = Uri.withAppendedPath(Uri.parse("content://com.android.contacts/raw_contacts"), String.valueOf(personId)); values = new ContentValues(); values.put("dirty", 0); c.update(u, values, null, null); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼
- Android使用CircleImageView實(shí)現(xiàn)圓形頭像的方法
- Android實(shí)現(xiàn)從本地圖庫/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android手機(jī)拍照或選取圖庫圖片作為頭像
- Android實(shí)現(xiàn)用戶頭像更換操作
- Android仿微信群聊頭像
- Android自定義控件仿QQ編輯和選取圓形頭像
- Android實(shí)現(xiàn)帶頭像的用戶注冊(cè)頁面
- Android自定義AvatarImageView實(shí)現(xiàn)頭像顯示效果
相關(guān)文章
Android開發(fā)的IDE、ADT、SDK、JDK、NDK等名詞解釋
這篇文章主要介紹了Android開發(fā)的IDE、ADT、SDK、JDK、NDK等名詞解釋,對(duì)這些概念搞不清楚是一件痛苦的事,本文就簡(jiǎn)潔講解了這些名詞的含義,一起掃盲吧,需要的朋友可以參考下2015-06-06AndroidStudio Gradle第三依賴統(tǒng)一管理的實(shí)現(xiàn)方法
這篇文章主要介紹了AndroidStudio Gradle第三依賴統(tǒng)一管理的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-09-09Android使用Kotlin和RxJava 2.×實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)效果
本篇文章主要介紹了Android使用Kotlin和RxJava 2.×實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)效果,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-12-12Android Fragment的回退棧示例詳細(xì)介紹
這篇文章主要介紹了Android Fragment的回退棧示例詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-12-12