以编程方式更改联系人图片 [英] Change contact picture programmatically

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

问题描述

我有一张照片,存储在android手机中。我希望能够更改联系人的图片。

I have a picture, that is stored into the android phone. I want to be able to change the picture of a contact.

到目前为止,我要做的是启动联系人选择器,让用户选择一个联系人,然后我得到所选联系人的URI。从此联系人中,我可以获取关联的rawContact,并且可以使用此代码

What I've done so far is launch the contact picker, have the user select a contact, and then I get the URI of the selected contact. From this contact, I can get the associated rawContact and I use this code.

Uri rawContactPhotoUri = Uri.withAppendedPath(
             ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
             RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
     try {
         AssetFileDescriptor fd =
             getContentResolver().openAssetFileDescriptor(rawContactPhotoUri, "rw");
         OutputStream os = fd.createOutputStream();
         os.write(photo);
         os.close();
         fd.close();
     } catch (IOException e) {
         // Handle error cases.
     }

问题是,AssetFIleDescriptor始终为空(当我在其上调用长度时) ,我们总是得到-1)。

The problem is, the AssetFIleDescriptor is always empty (when I call length on it, we always get -1).

我并不需要整个解决方案,只需遵循一些线索即可帮助我实现这一目标。我似乎无法在StackOverflow上找到此问题,因此将不胜感激。

I'm not asking for the entire solution, just some leads to follow that can help me to get that working. I cannot seem to find this problem already on StackOverflow, so any help would be appreciated.

编辑

总是在我们提出问题时才找到解决方案。
我想与其他人共享它

It's always when we ask for question that we find the solution. I want to share it for other

所以我放弃了android链接并找到另一个链接:
http://wptrafficanalyzer.in/blog/programatically-adding-联系人在Android中使用照片/联系人提供者示例/

So I gave up on android link and find another one : http://wptrafficanalyzer.in/blog/programatically-adding-contacts-with-photo-using-contacts-provider-in-android-example/

图片选择器返回所选联系人的Uri,因此这可以得到它的Contact._ID:

The picture picker return the Uri of the selected contact, so with this you can get the Contact._ID of it :

// This is onActivityResult
final Uri uri = data.getData();
final Cursor cursor1 = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
final long contactId = cursor1.getLong(cursor1.getColumnIndex(Contacts._ID);
cursor1.close();

然后我必须获取RawContactId:

Then I had to get the RawContactId :

final Cursor cursor2 = getContentResolver().query(RawContacts.CONTENT_URI, null,     RawContacts.Contact_ID + "=?", new String[] {String.valueOf(contactId)}, null);
cursor2.moveToFirst();
final long rawContactId = cursor2.getLong(cursor2.getColumnIndex(RawContacts._ID));
cursor2.close();

然后我必须获取RawContacts的Data._ID(与上面相同)。

Then I had to get the Data._ID of the RawContacts (same way as above).

然后我使用ContentProviderOperations:

Then I used the ContentProviderOperations :

final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
    .withSelection(Data._ID, dataId),
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArrayOfThePicture);

getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

这就像魅力一样。希望对您有帮助

And this is working like charm. Hope it helps

推荐答案

String contactId ="10001"; // change it as your IDs
    if (mBitmap != null) {
                // Picture
                try {
                    ByteArrayOutputStream image = new ByteArrayOutputStream();
                    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, image);

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

                    ContentValues values = new ContentValues();
                    int photoRow = -1;
                    String where111 = ContactsContract.Data.RAW_CONTACT_ID + " == " +
                            ContentUris.parseId(rawContactUri) + " AND " + ContactsContract.Data.MIMETYPE + "=='" +
                            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
                    Cursor cursor = managedQuery(
                            ContactsContract.Data.CONTENT_URI,
                            null,
                            where111,
                            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, image.toByteArray());
                    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);
                    }
                } catch (Exception e) {
                    Log.e("!_@@Image_Exception", e + "");
                }
            }
 try {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
       } catch (Exception e) {
            Log.e("@@@@@UPLOADERR", e + "");
        }

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

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