【问题标题】:Android contacts High-Res DisplayPhoto not showing upAndroid 联系人高分辨率 DisplayPhoto 未显示
【发布时间】:2019-08-30 13:40:34
【问题描述】:

我正在使用开发指南中的方法将高分辨率显示照片保存到 Android 联系人中:https://developer.android.com/reference/kotlin/android/provider/ContactsContract.RawContacts.DisplayPhoto

保存后的联系人表如下所示:

------------------------------------------------------------------------------------------------------------------------------------------------------------------
| *** Contacts table ***                                                                                                                                         |
------------------------------------------------------------------------------------------------------------------------------------------------------------------
| _id | raw_contact_id | display_name | photo_id | file_id |                  photo_thumb_uri                  |                    photo_uri                    |
------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 661 |    661         | ContactName  |   6125   |    26   | content://com.android.contacts/contacts/661/photo | content://com.android.contacts/display_photo/26 |
------------------------------------------------------------------------------------------------------------------------------------------------------------------

PhotoUri 是正确的,我确实可以使用该 URI 加载高分辨率照片。

但是,片刻之后,Contact Synchronization Service 启动了:

2019-08-30 15:17:42.991 14029-19208/? W/FSA2_ContactsSyncAdapter: @onPerformSync Sync started
2019-08-30 15:17:43.056 14029-19208/? W/ChimeraUtils: Non Chimera context
2019-08-30 15:17:43.056 14029-19208/? W/ChimeraUtils: Non Chimera context

2019-08-30 15:17:43.721 14029-19208/? I/FSA2_SyncState: @readSyncState: # aohd@ea156530

2019-08-30 15:17:44.133 14029-19208/? I/FSA2_SyncState: @readSyncState: # aohd@4c3cbbea

2019-08-30 15:17:44.335 14029-19208/? E/PhotoUrlUtil: Photo cell is empty.

2019-08-30 15:17:45.242 14029-19208/? I/FSA2_SyncUpPhotoCursor: Start to upload photo for contact 74ae94138cec7df6
2019-08-30 15:17:45.248 14029-19208/? E/PhotoUrlUtil: Photo cell is empty.
2019-08-30 15:17:45.248 14029-19208/? E/PhotoUrlUtil: Photo cell is empty.

2019-08-30 15:17:47.016 14029-19208/? W/FSA2_ContactsSyncAdapter: @onPerformSync Sync finished successfully

同步后联系人表不正确,没有引用高分辨率照片,而只引用缩略图(低分辨率)照片。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------
| *** Contacts table ***                                                                                                                                           |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
| _id | raw_contact_id | display_name | photo_id | file_id |                  photo_thumb_uri                  |                    photo_uri                      |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 661 |    661         | ContactName  |   6125   |   null  | content://com.android.contacts/contacts/661/photo | content://com.android.contacts/contacts/661/photo |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

我无法再使用联系人表中的 URI 读取高分辨率数据。我用于阅读以下方法:

//true is for high-res photos
ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, contactUri, true)

同时,“Google 通讯录”应用程序能够加载高分辨率的联系人照片。当我启动 Google 联系人应用程序时,稍后会在联系人详细信息视图中显示高分辨率照片。在 Google 应用中打开联系人后,我可以再次在联系人表中看到正确的条目。

据我了解,在 Google 应用中显示联系人是在 QuickContactActivity 中实现的:

https://android.googlesource.com/platform/packages/apps/Contacts/+/refs/heads/master/src/com/android/contacts/quickcontact/QuickContactActivity.java#1120

我想了解保存(或加载)联系人高分辨率照片的正确方法是什么。对我来说,得到一个高级描述就足够了。我调查了 Google 通讯录应用程序源代码,但我找不到任何简单的线索,它们是如何完成的。提供基于 Google 应用的简单算法配方也算作一种解决方案。

我正在 Android 模拟器上测试应用程序,API 级别 28,最低。 21级。

附加信息: 在使用 Google 联系人应用程序保存联系人照片时,我可以观察到相同的过程(如上所述)。因此,似乎在读取照片时解析高分辨率照片信息 - 保存不是问题。

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    好的,我终于找到了正确的解决方案。基本上,在查看高分辨率照片之前,您应该在同步适配器服务中触发联系人照片的同步。同步完成后,您可以检索高分辨率照片并在您的 UI 上进行设置。在同步之前,您通常只能使用缩略图质量的照片。

    现在调度意图以启动高分辨率照片同步(Kotlin)的功能:

    fun dispatchSyncHighResPhotoIntent(context: Context, rawContactId: Long) {
        val uri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId)
        val intent = Intent()
        intent.setDataAndType(uri, ContactsContract.RawContacts.CONTENT_ITEM_TYPE)
        intent.component = ComponentName(
            "com.google.android.syncadapters.contacts",
            "com.google.android.syncadapters.contacts.SyncHighResPhotoIntentService"
        )
        intent.action = "com.google.android.syncadapters.contacts.SYNC_HIGH_RES_PHOTO"
        context.startService(intent)
    }
    

    在调度意图之前,您可以设置观察者,它会在同步完成后更新您的 UI。您可以在下面找到在这种情况下有用的方法。搜索相应的文档以获取更多详细信息。

    
    //For registering observer...
    //uri - should be the same uri as uri calculated in dispatchSyncHighResPhotoIntent() function
    //true - if should notify for descendants
    //observer - your implementation of ContentObserver, which e.g. will update UI
    
    context.contentResolver.registerContentObserver(uri, true, observer)
    
    
    //For unregistering observer...
    context.contentResolver.unregisterContentObserver(observer)
    
    

    同步完成后,应按照文档中的方式读取联系人的高分辨率照片,或者例如通过将 Glide 与添加到 Data 表中的 URI 一起使用。

    //Reading contact high-res photo as per Android documentation; true is for high-res photos
    ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, contactUri, true)
    

    【讨论】:

    • registerContentObserver 不是注册所有更改并且有很多误报吗?你怎样才能让它被过滤,只在你需要的时候使用它,并且只有在它真正改变时才使用它?另外我认为不需要intent.component 的行,因为您可以看到联系人应用程序在没有这部分的情况下处理它:android.googlesource.com/platform/packages/apps/Contacts.git/+/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    相关资源
    最近更新 更多