0

I want to retrieve a user's profile and it's image, but this is not working. I always get an empty cursor (cursor.getCount() == 0). Can someone help?

I have a profile with an image and a phone number on my phone but I can't read it. Permissions (read and write contacts permissions) are granted and I can retrieve all my phone contacts, but not the own profile.

Any ideas?

Code

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

Additional info

I think this code worked on my S6 with android 7 but it's not working on my new S9 with android 8 on it (can't test it on my old phone anymore as it's not working anymore). So this may be an android version specific problem...

prom85
  • 16,896
  • 17
  • 122
  • 242
  • Just tested your code, and it works fine on my S9 running Android 8. We have a similar code in our app and it works fine to the best of my knowledge... BTW you're `selection` variable is actually the `projection`, and you don't need all those `getColumnIndex` they're just the order in which they're specified in your projection – marmor Aug 21 '18 at 11:47
  • Thanks for testing. I'm aware of the other things, but thanks for pointing out. Still weird, that I don't get my own profile on the same phone... – prom85 Aug 21 '18 at 16:41
  • are you seeing your profile under the Samsung contacts app? – marmor Aug 22 '18 at 07:10
  • Yes I am. And that's where I've set it up and added a number and a picture. It's there on top under contacts underneath the "me" header... code seems to work on other devices, got this feedback from my beta testers in the meantime, with no problems as well... – prom85 Aug 22 '18 at 16:38
  • can you join me here, so we can check if the code in my app works properly on your device? https://chat.stackoverflow.com/rooms/178584/android-contacts-user-profile – marmor Aug 23 '18 at 06:56

1 Answers1

1

This appears to be bad implementation of Samsung's Contacts app, I've opened a bug report on their developer's forum here: https://developer.samsung.com/forum/thread/contacts-app-profile-is-not-accessible-via-contactscontractprofile-api/201/354874

marmor
  • 27,641
  • 11
  • 107
  • 150