2012-07-15 148 views

回答

1

嘗試

轉換位圖到的bytearray

Bitmap bit; // <-- put your bitmap here 
ByteArrayOutputStream streamy = new ByteArrayOutputStream(); 
bit.compress(CompressFormat.PNG, 0, streamy); 
byte[] photo = streamy.toByteArray(); 

然後

ContentValues values = new ContentValues(); 
int photoRow = -1; 
String where = ContactsContract.Data.RAW_CONTACT_ID + " == " + 
    ContentUris.parseId(yourContectID) + " AND " + Data.MIMETYPE + "=='" + 
    ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'"; 
Cursor cursor = managedQuery(
     ContactsContract.Data.CONTENT_URI, 
     null, 
     where, 
     null, 
     null); 
int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID); 
if(cursor.moveToFirst()){ 
    photoRow = cursor.getInt(idIdx); 
} 
cursor.close(); 
values.put(ContactsContract.Data.RAW_CONTACT_ID, 
     ContentUris.parseId(yourContectID)); 
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1); 
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, photo); 
values.put(ContactsContract.Data.MIMETYPE, 
     ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE); 
if(photoRow >= 0){ 
    this.getContentResolver().update(
      ContactsContract.Data.CONTENT_URI, 
      values, 
      ContactsContract.Data._ID + " = " + photoRow, null); 
    } else { 
     this.getContentResolver().insert(
       ContactsContract.Data.CONTENT_URI, 
       values); 
    } 
} 

不要忘記填寫您的manifest file

+0

添加權限WRITE_CONTACTSREAD_CONTACTS這是一個複製粘貼事情。我看過這篇文章。 – rockydgeekgod 2012-07-15 12:42:49

+0

請根據我的問題進行編輯。 – rockydgeekgod 2012-07-15 12:43:08

+0

你的問題是什麼? – 2012-07-15 12:48:10

相關問題