Android change contact picture

2019-01-15 10:58发布

I'm building an app where when a image is clicked the user sees the contacts list and picks one. After clicking on it, it's contact picture should change to the image clicked in the first place. Here's how i implement it:

....
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, SELECT_CONTACT);
.....
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_CONTACT) {
                Uri contactData = data.getData();
                ????? what should come here???
            }
        }
    }

My question is how do i acces and change the contact picture? Thank you

1条回答
趁早两清
2楼-- · 2019-01-15 11:18

First, get the Uri for the Contacts first raw contact:

Uri rawContactUri = null;
Cursor rawContactCursor =  managedQuery(
        RawContacts.CONTENT_URI, 
        new String[] {RawContacts._ID}, 
        RawContacts.CONTACT_ID + " = " + contactData.getLastPathSegment(), 
        null, 
        null);
if(!rawContactCursor.isAfterLast()) {
    rawContactCursor.moveToFirst();
    rawContactUri = RawContacts.CONTENT_URI.buildUpon().appendPath(""+rawContactCursor.getLong(0)).build();
}
rawContactCursor.close();

Then, convert a bitmap to a byte array:

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

Finally, set the byte array as the raw contact's photo:

ContentValues values = new ContentValues(); 
int photoRow = -1; 
String where = ContactsContract.Data.RAW_CONTACT_ID + " == " + 
    ContentUris.parseId(rawContactUri) + " 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(rawContactUri)); 
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); 
    } 
} 

EDIT

Be sure to include these two permissions in your manifest:

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
查看更多
登录 后发表回答