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

问题描述

这似乎适用于小图像:

ContentValues values = new ContentValues();

values.put(ContactsContract.Data.RAW_CONTACT_ID, id);
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) {
    context.getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID + " = " + photoRow, null);
} else {
    context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
}

文档中,我意识到对于大图像,我需要设置PHOTO_FILE_ID ,所以我可以替换:

From the docs I realise that for large images I need to set the PHOTO_FILE_ID, so I can replace:

ContactsContract.CommonDataKinds.Photo.PHOTO

具有:

ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID

但是,我需要提供一个PHOTO_FILE_ID而不是原始数据.我的问题:

However, then I need to supply a PHOTO_FILE_ID rather than raw data. My question:

  1. 如何保存照片(字节[])并获取PHOTO_FILE_ID?
  2. 如果已经有照片(照片不是PHOTO_FILE_ID).我需要删除它才能看到大图像还是要优先处理大图像?如果没有,如何删除它?

推荐答案

您自己的答案会起作用,但是效率不高,因为需要将照片编码为SQL查询并通过Android IPC传输.这也使它受制于Android的IPC大小限制(1MB)(即,如果您的照片太大,则内容提供商操作将失败).

Your own answer will work, but it's not very efficient because the photo needs to be encoded into an SQL query and piped through Android IPC. That also makes it a subject to Android's IPC size limit of 1MB (i.e. if your photo is too large the content provider operation will fail).

设置(创建或覆盖)RawContact(原始)照片的最有效方法是使用和public void writeDisplayPhoto(long rawContactId, byte[] photo) { 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. } }

此方法的唯一缺点是,它总是创建/替换RawContact的主照片.如果RawContact还没有照片,它将添加一张照片.

The only drawback of this approach is that it always creates/replaces the primary photo of the RawContact. If the RawContact doesn't have a photo yet this will add one.

很遗憾,无法将openAssetFileDescriptorPHOTO_FILE_ID结合使用,因此您无法使用此方法覆盖由其ID标识的特定照片.但是,在现实生活中,大多数联系人最多只能有一张照片,所以这并不是一个真正的限制.

Unfortunately there is no way to use openAssetFileDescriptor with a PHOTO_FILE_ID, so you can't override a specific photo identified by its ID using this method. However, in real life most contacts probably have at most one photo, so that's not a real limitation.

这将自动更新 Photo.PHOTO 列大照片的缩略图,然后指定PHOTO_FILE_ID.

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

10-22 17:37