以编程方式更改联系人图片

5
我有一张照片,存储在安卓手机上。我想要更改联系人的图片。
到目前为止,我已经启动了联系人选择器,让用户选择联系人,然后我获取所选联系人的URI。从这个联系人中,我可以获取相关的rawContact,并使用此代码:http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.DisplayPhoto.html。
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总是为空(当我对其调用length时,我们总是得到-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的临时读取权限,但是您需要设置WRITE_CONTACTS权限才能实际更新联系人。 - Josiah
我拥有读取和写入联系人权限。 - Tristan
1个回答

3
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 + "");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接