首页 > 解决方案 > 无法检索用户个人资料

问题描述

我想检索用户的个人资料及其图像,但这不起作用。我总是得到一个空光标(cursor.getCount() == 0)。有人可以帮忙吗?

我的手机上有一张带有图片和电话号码的个人资料,但我无法阅读。授予权限(读取和写入联系人权限),我可以检索我所有的电话联系人,但不能检索自己的个人资料。

有任何想法吗?

代码

void loadUser() {
    Uri dataUri = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
    String[] selection = new String[]
            {
                    ContactsContract.Data.RAW_CONTACT_ID,
                    ContactsContract.Data._ID,
                    ContactsContract.Profile.DISPLAY_NAME,
                    ContactsContract.Profile.PHOTO_URI,
                    ContactsContract.Profile.LOOKUP_KEY,
                    ContactsContract.Data.DATA_VERSION
            };
    Cursor cursor = MainApp.get().getContentResolver().query(
            dataUri,
            selection,
            null,
            null,
            null);

    if (cursor != null) {

        L.d("MY PROFILE - cursor size: %d", cursor.getCount());

        int rawId = cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
        int id = cursor.getColumnIndex(ContactsContract.Data._ID);
        int name = cursor.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME);
        int photoUri = cursor.getColumnIndex(ContactsContract.Profile.PHOTO_URI);
        int lookupKey = cursor.getColumnIndex(ContactsContract.Profile.LOOKUP_KEY);
        int version = cursor.getColumnIndex(ContactsContract.Data.DATA_VERSION);

        try {
            if (cursor.moveToFirst()) {
                long phRawId = cursor.getLong(rawId);
                int phId = cursor.getInt(id);
                String phName = cursor.getString(name);
                String phImageUri = cursor.getString(photoUri);
                String phLookupKey = cursor.getString(lookupKey);
                int phVersion = cursor.getInt(version);
                boolean phExists = true;

                L.d("MY PROFILE - RawID: %d, ID: %d", phRawId, phId);

                // ... profile successfully retrieved
            } else {
                L.d("MY PROFILE - cursor is EMPTY");
            }
        } finally {
            cursor.close();
        }
    } else {
        L.d("MY PROFILE - cursor = NULL");
    }
}

附加信息

认为这段代码在我的带有 android 7 的 S6 上工作,但它不能在我的带有 android 8 的新 S9 上工作(不能再在我的旧手机上测试它,因为它不再工作了)。所以这可能是一个特定于android版本的问题......

标签: androidandroid-contactscontactscontract

解决方案


这似乎是三星联系人应用程序的错误实施,我在他们的开发者论坛上打开了一个错误报告:https ://developer.samsung.com/forum/thread/contacts-app-profile-is-not-accessible- via-contactscontractprofile-api/201/354874


推荐阅读