1

I try to get a contact detail. I can get DISPLAY_NAME, Phone.DATA from Phone.CONTENT_URI. I use below codes :

String urlData = "";
    final String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Website.URL,
            ContactsContract.CommonDataKinds.Website.TYPE
    };
    final Cursor contactData = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=" + contactId,null, null);

    if (contactData.moveToNext()) {

        int urlColumnIndex = contactData.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL);
        String url = contactData.getString(urlColumnIndex);
        String urlType = contactData.getString(contactData.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE));
        urlData += url + " " + urlType;
        return urlData;
    }

It returns the value of display_name. I don't understand why I cannot get url data.

How can I do it?

EDIT

I have Contacts._ID.

String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

I use below function to get contact url address.

private void getURLById(String id, ContentResolver cr) {
    Log.i("@@getURLById", ".");

    final String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Website.URL,
            ContactsContract.CommonDataKinds.Website.TYPE
    };

    String selection = ContactsContract.Data._ID + " = " + id + " AND " + ContactsContract.Contacts.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'";

    final Cursor websites = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, null, null);

    Log.e("Count ", " " + websites.getCount());

Count returns zero.

  • Not sure what you are trying to do, but if it helps, you need to use Threads to access a website through android – 131 Feb 11 '17 at 09:56
  • I just want to get contact url data. Why I need to use threads? –  Feb 11 '17 at 09:58

1 Answers1

2

keep the query for Data table, and add the following selection: Data.CONTACT_ID + " = " + contactId + " and " + Data.MIMETYPE + " = '" + CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'"

this will select only rows of type Website.

Code:

final String[] projection = new String[] {
        ContactsContract.CommonDataKinds.Website.URL,
        ContactsContract.CommonDataKinds.Website.TYPE
};
String selection = Data.CONTACT_ID + " = " + contactId + " AND " + Data.MIMETYPE + " = '" + CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'";

final Cursor contactData = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);

UPDATE

Contacts._ID is not Data._ID, change your selection to:

String selection = ContactsContract.Data.CONTACT_ID + " = " + id + " AND " + ContactsContract.Contacts.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'";
marmor
  • 27,641
  • 11
  • 107
  • 150
  • There is no table named Website.CONTENT_URI. I do not access it. –  Feb 11 '17 at 16:29
  • then see my edit, added another way of getting the same result – marmor Feb 11 '17 at 20:44
  • yes there is, see here: https://developer.android.com/reference/android/provider/ContactsContract.Data.html#CONTENT_URI make sure you're importing the correct `Data` table: `android.provider.ContactsContract.Data` – marmor Mar 13 '17 at 11:32
  • see my answer update, `Data._ID` is the ID of the data row, not the `contact-id`, you need `Data.CONTACT_ID`. – marmor Mar 13 '17 at 12:14