4

I am using ContactsContract api intent for showing all contacts from an activity. This intent returns an id of the contact. I need to get the postal address of this contact.

Here is the code which i am using for showing contacts:

Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(ContactsContract.Contacts.CONTENT_TYPE); startActivityForResult(intent, PICK_CONTACT);

I am getting the result in the onActivityResult function.

Please help, how do i do this.

mudit
  • 25,306
  • 32
  • 90
  • 132

4 Answers4

10

in your onActivityResult, first get the contact ID and query the ContactsContract.Data for the relevant data:

// get the contact ID

Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
cursor.moveToFirst();
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
cursor.close();

// get the data package containg the postal information for the contact
cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, 
    new String[]{ StructuredPostal.STREET,
        StructuredPostal.CITY,
// add more coluns from StructuredPostal if you need them
        StructuredPostal.POSTCODE},
        ContactsContract.Data.CONTACT_ID + "=? AND " +
            StructuredPostal.MIMETYPE + "=?",
        new String[]{String.valueOf(id), StructuredPostal.CONTENT_ITEM_TYPE},
        null);


Street = cursor.getString(cursor.getColumnIndex(StructuredPostal.STREET));
Postcode = cursor.getString(cursor.getColumnIndex(StructuredPostal.POSTCODE));
City = cursor.getString(cursor.getColumnIndex(StructuredPostal.CITY)));
// etc. 
Thorstenvv
  • 5,376
  • 1
  • 34
  • 28
2

Can be done this way too (little lesser code)

Send the Intent this way:

Intent intent = new Intent(Intent.ACTION_PICK);

intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_TYPE); startActivityForResult(intent, PICK_CONTACT);

In the onActivityResult method

Uri contactData = data.getData();
Cursor c =  managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
    String address = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
}
Kiran
  • 814
  • 9
  • 6
1

This will return all the addresses from the contact Postal Address.

     Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
     String sortOrder = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
      Cursor cr = getContentResolver().query(uri,null, null, null, sortOrder);
      while(cr.moveToNext())
{
    String  Street = cr.getString(cr.getColumnIndex(StructuredPostal.STREET));
    String  Postcode = cr.getString(cr.getColumnIndex(StructuredPostal.POSTCODE));
    String  City = cr.getString(cr.getColumnIndex(StructuredPostal.CITY));
    Toast.makeText(this,"Address is : " +City+","+Street ,Toast.LENGTH_SHORT).show();
      }
      cr.close();
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
1

Kiran's answer probably works fine for Android 2.0 and above, but when you want it to be compatible for Android 1.6 and below you need to adapt the code a bit:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(Contacts.ContactMethods.CONTENT_POSTAL_TYPE);
startActivityForResult(intent, PICK_CONTACT);

And in onActivtyResult method:

Uri contactData = data.getData();
Cursor c =  managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
   String address = c.getString(c.getColumnIndexOrThrow(Contacts.ContactMethodsColumns.DATA));
}

Where PICK_CONTACT is a int of your own choice. (Right?)

Kasium
  • 985
  • 10
  • 24