【问题标题】:Change contact picture programmatically以编程方式更改联系人图片
【发布时间】:2013-07-22 13:47:53
【问题描述】:

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

到目前为止,我所做的是启动联系人选择器,让用户选择一个联系人,然后我得到所选联系人的 URI。从这个联系人,我可以得到相关的 rawContact,我使用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)。

我并不是要求提供完整的解决方案,只是需要一些线索来帮助我实现该解决方案。我似乎在 StackOverflow 上找不到这个问题,因此我们将不胜感激。

编辑

我们总是在提出问题时找到解决方案。 我想分享给其他人

所以我放弃了 android 链接并找到另一个链接: http://wptrafficanalyzer.in/blog/programatically-adding-contacts-with-photo-using-contacts-provider-in-android-example/

图片选择器返回所选联系人的 Uri,因此您可以获取联系人的 Contact._ID:

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

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(与上面相同)。

然后我使用了 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);

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

【问题讨论】:

  • 您在 AndroidManifest.xml 中有哪些权限?联系人选择器对检索到的 URI 提供临时 READ 权限,但您需要设置 WRITE_CONTACTS 权限才能真正更新联系人。
  • 我拥有 READ 和 WRITE_CONTACT 权限。

标签: java android image contact


【解决方案1】:
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 + "");
        }

【讨论】:

    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    相关资源
    最近更新 更多