在android上更改联系人的图片?

时间:2011-12-16 21:51:28

标签: android contacts

我正在尝试更改android上的联系人照片,我正在使用此处的代码: http://groups.google.com/group/android-developers/msg/7798b51e01c61c1e

但它不起作用.. 我正在显示联系人列表,当用户点击其中一个联系人时,它的照片应该更改为资源中的照片。 这是我的整个代码:

public class ContactFacesActivity extends ListActivity {

    ArrayList<Contact> Contacts;
    ContentResolver cr;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Contacts = new ArrayList<Contact>();



        cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Contacts.add(new Contact(name, id));
                }
            }

        setListAdapter(new ArrayAdapter<Contact>(ContactFacesActivity.this, android.R.layout.simple_list_item_1, Contacts));

        }
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Bitmap temp = ((BitmapDrawable)getResources().getDrawable(R.drawable.house)).getBitmap();

        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
        temp.compress(CompressFormat.JPEG, 90 , bos); 
        byte[] bitmapdata = bos.toByteArray();

        setPhoto(Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_URI, Contacts.get(position).getID()), bitmapdata);
    }

    public void setPhoto(Uri personUri, byte[] photo) { 
                ContentValues values = new ContentValues(); 
                int photoRow = -1; 
                String where = ContactsContract.Data.RAW_CONTACT_ID + " == " + 
            ContentUris.parseId(personUri) + " AND " + Data.MIMETYPE + "=='" + 
            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'"; 
                Cursor cursor = getContentResolver().query 
            (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(personUri)); 
                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){ 
                    getContentResolver().update 
            (ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID 
            + " = " + photoRow, null); 
                } else { 
                    getContentResolver().insert 
            (ContactsContract.Data.CONTENT_URI, values); 
                } 
            } 

}

1 个答案:

答案 0 :(得分:2)

获取你的Uri联系人:

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

然后到位图

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(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); 
    } 
} 

清单

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>