tchoum
tchoum

Reputation: 332

Using Entity Uri to get Contact Infos on Sony Xperia SP

I'm developing an App dealing with Contacts and Calendar.

I'm having a hard time with a specific device, the Sony Xperia SP, but I couldn't find anyone else having the same problem.

I'm trying to get the Display Name, and Phone numbers of my device's contacts, using the Entity Uri available in the Android Api : http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.Entity.html

Unfortunately, this code is working well on a Samsung Galaxy S4 advance, where I get all the details I'm querying, But on the Sony Xperia SP, I'm getting an illegalargumentexception, detailed in the stacktrace by a : invalid column name display name.

Below is the code I'm using:

ContentResolver cr = context.getContentResolver();
Uri rawUri = RawContacts.CONTENT_URI;
String[] rawProjection = { RawContacts._ID };
String rawSelection = RawContacts.ACCOUNT_NAME + " = ? AND " + RawContacts.ACCOUNT_TYPE + " = ?";
String[] rawSelectionArgs = new String[] { account.name, account.type };

    Cursor rawCursor = cr.query(rawUri, rawProjection, rawSelection, rawSelectionArgs, null);

    while (rawCursor.moveToNext()) {

        long contactId = rawCursor.getLong(0);

        Uri entityUri = Uri.withAppendedPath(ContentUris.withAppendedId(rawUri, contactId), Entity.CONTENT_DIRECTORY);
        String[] entityProjection = new String[] { Data.DISPLAY_NAME, CommonDataKinds.Phone.NORMALIZED_NUMBER };
        String entitySelection = Entity.MIMETYPE + " = ?";
        String[] entitySelectionArgs = new String[] { CommonDataKinds.Phone.CONTENT_ITEM_TYPE };
        Cursor entityCursor = cr.query(entityUri, entityProjection, entitySelection, entitySelectionArgs, null);

        while (entityCursor.moveToNext()) {
            String name = entityCursor.getString(entityCursor.getColumnIndex(Data.DISPLAY_NAME));
            String number = entityCursor.getString(entityCursor.getColumnIndex(CommonDataKinds.Phone.NORMALIZED_NUMBER));
        }

        entityCursor.close();

    }

I've tried to put the projection of my entityQuery as null, to look for what fields are returned by this query, and indeed, the fields returned are not the same depeding on the device,

Does anyone knows why these fields are not visible on the Sony Xperia SP, and which workaround I could use there?

Upvotes: 1

Views: 295

Answers (1)

Smeet
Smeet

Reputation: 4116

Make sure that you have given read contacts permission. I have same issue, when selecting contact.

<uses-permission android:name="android.permission.READ_CONTACTS" />

Upvotes: -1

Related Questions