Android的改变联系人图片 [英] Android change contact picture

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

问题描述

我要建一个应用程序,其中当图像被点击的用户所看到的联系人列表,并选出一个。点击它后,它的联系人图片应该改变形象单击摆在首位。 以下是我实现它:

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

推荐答案

首先,获得的URI联系人第一次生联系方式:

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

修改

一定要包括在清单这两个权限:

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"/>

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆