更改联系人的图片在Android

更改联系人的图片在Android

本文介绍了更改联系人的图片在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变联系人的照片在Android上,我使用的是code从这里:
的?

不过,这并不工作..
我显示该联系人的列表,并且当用户点击联系人之一,它的照片应该被改变为照片这是在资源。
这里是我的整个code:

 公共类ContactFacesActivity扩展ListActivity {    ArrayList的<联系与GT;联系人;
    ContentResolver的铬;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        联系方式=新的ArrayList<联系与GT;();        CR = getContentResolver();
        光标CUR = cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);
        如果(cur.getCount()大于0){
        而(cur.moveToNext()){
            字符串ID = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            字符串名称= cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            如果(的Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))大于0){
                Contacts.add(新的联系方式(姓名,身份证));
                }
            }        setListAdapter(新ArrayAdapter<联系与GT;(ContactFacesActivity.this,android.R.layout.simple_list_item_1,联系人));        }
    }
    @覆盖
    保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
        super.onListItemClick(L,V,位置ID);        位图TEMP =((BitmapDrawable)getResources()getDrawable(R.drawable.house))getBitmap()。        ByteArrayOutputStream BOS =新ByteArrayOutputStream();
        temp.com preSS(比较pressFormat.JPEG,90,BOS);
        字节[] =的BitmapData bos.toByteArray();        setPhoto(Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,Contacts.get(位置).getID()),的BitmapData);
    }    公共无效setPhoto(URI personUri,字节[]的照片){
                ContentValues​​值=新ContentValues​​();
                INT photoRow = -1;
                字符串,其中= ContactsContract.Data.RAW_CONTACT_ID +==+
            ContentUris.parseId(personUri)+和+ Data.MIMETYPE +=='+
            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE +';
                光标光标= getContentResolver()。查询
            (ContactsContract.Data.CONTENT_URI,空,其中,NULL,NULL);
                INT idIdx = cursor.getColumnIndexOrThrow
            (ContactsContract.Data._ID);
                如果(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,照片);
                values​​.put(ContactsContract.Data.MIMETYPE,
            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
                如果(photoRow> = 0){
                    getContentResolver()更新
            (ContactsContract.Data.CONTENT_URI,价值观,ContactsContract.Data._ID
            +=+ photoRow,NULL);
                }其他{
                    getContentResolver()。插入
            (ContactsContract.Data.CONTENT_URI,价值观);
                }
            }}


解决方案

获取UR乌里联系方式:

 乌里iContactUri = NULL;
    光标iContactCursor = managedQuery(
            RawContacts.CONTENT_URI,
            新的String [] {} RawContacts._ID,
            RawContacts.CONTACT_ID +=+ contactData.getLastPathSegment(),
            空值,
            空值);
    如果(!iContactCursor.isAfterLast()){
        iContactCursor.moveToFirst();
        。iContactUri = RawContacts.CONTENT_URI.buildUpon()appendPath(+ iContactCursor.getLong(0))构建();
    }
    ContactCursor.close();

再到位图

 位图位; //<  - 把你的位图在这里
ByteArrayOutputStream流j =新ByteArrayOutputStream();
bit.com preSS(比较pressFormat.PNG,0,流j);
字节[] =照片streamy.toByteArray();

照片

  ContentValues​​值=新ContentValues​​();
INT photoRow = -1;
字符串,其中= ContactsContract.Data.RAW_CONTACT_ID +==+
    ContentUris.parseId(rawContactUri)+和+ Data.MIMETYPE +=='+
    ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE +';
光标光标= managedQuery(
        ContactsContract.Data.CONTENT_URI,
        空值,
        哪里,
        空值,
        空值);
INT idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
如果(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,照片);
values​​.put(ContactsContract.Data.MIMETYPE,
        ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
如果(photoRow> = 0){
    this.getContentResolver()。更新(
            ContactsContract.Data.CONTENT_URI,
            值,
            ContactsContract.Data._ID +=+ photoRow,NULL);
    }其他{
        this.getContentResolver()。插入(
                ContactsContract.Data.CONTENT_URI,
                值);
    }
}

清单

 <使用许可权的android:NAME =android.permission.WRITE_CONTACTS/>
<使用许可权的android:NAME =android.permission.READ_CONTACTS/>

I'm trying to change contact's photo on android, I'm using the code from here:http://groups.google.com/group/android-developers/msg/7798b51e01c61c1e?

But it doesn't work..I'm displaying a list of the contacts, and when a user clicks one of the contacts, it's photo supposed to be changed to a photo which is in the resources.Here's my entire code:

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

}
解决方案

Get ur Uri Contacts :

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

then to bitmap

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

Photos

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

Manifest

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

这篇关于更改联系人的图片在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:32