androidandroid-intentandroid-contacts

Contact Not Found on Intent with Contact Application


What I want is to go to the profile of the contact in the Contact application when I press an item on a list :

viewHolder.swipeLayout.setOnLongClickListener(new SwipeLayout.LongClickListener() {

        public void onLongPress(View view) {

            Intent goContactIntent = new Intent(Intent.ACTION_VIEW);
            String contactID = mData.get(position).getId();
            Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactID);
            Toast.makeText(mContext,"uri : "+uri, Toast.LENGTH_LONG).show();
            goContactIntent.setData(uri);
            view.getContext().startActivity(goContactIntent);
        }
    });

I retrieve the list of all the contacts (name, phone number and ID) with the getContacts() when the application starts. At least the names and phone numbers work.

public  ArrayList<Contact> getContacts() {
    //Adress of the table in the database
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    //Retrieve data
    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};

    //Initialize the cursor
    Cursor people = getContentResolver().query(uri, projection, null, null, null);

    int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    int indexId = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);

    ArrayList<Contact> contacts = new ArrayList<Contact>();

    //Shaping data
    people.moveToFirst();
    do {
        String name   = people.getString(indexName);
        String number = people.getString(indexNumber);
        String contactid = people.getString(indexId);

        Contact contact1 = new Contact();

        contact1.setFirstName(name);
        contact1.setPhoneNumber(number);
        contact1.setId(contactid);

        contacts.add(contact1);

    } while (people.moveToNext());

    return contacts;
}

In the onLongPress function, when I toast, I get URIs like : "content://com.android.contacts/contacts/780" and a toast (I didn't implement) : "Contact not found"

I know how to go to the Contact application (I have code that works for that), but I can't go on a specific contact profile.


Solution

  • The id you passed in the URI is wrong I think. I am using ContactsContract.RawContacts.CONTACT_ID and it works absolutely fine.