Get Contact Info from Android Contact Picker

get contact info from android contact picker

Phone Numbers

Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.

    if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phones
}
pCur.close();
}

Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.

Email Addresses

Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table.

Cursor emailCur = cr.query( 
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();

As with the phone query the field names for the email table are also stored under ContactsContract.CommonDataKinds. The email query is performed on the URI in ContactsContract.CommonDataKinds.Email.CONTENT_URI and the WHERE clause has to match the ContactsContract.CommonDataKinds.Email.CONTACT_ID field. Since multiple email addresses can be stored loop through the records returned in the Cursor.

More tutorials here

This method requires Android API version 5 or higher.

Contact picker, get phone number

This worked for me.

Uri contactData = data.getData();
Cursor c = main.managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
}

Cursor c1 = mcontext.getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(contactId)}, null);
c1.moveToFirst();
String number = c1.getString(1);

I got the query from the google documentation. You get string at position one because that's the position in the query above.

http://developer.android.com/reference/android/provider/ContactsContract.Data.html

Android contact picker: pick phone or email

it's already been answered : Link to answer
but i will just post the code here

private void contactPicked(Intent data) {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
cur.moveToFirst();
try {
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cur = getContentResolver().query(uri, null, null, null, null);
cur.moveToFirst();
// column index of the contact ID
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
// column index of the contact name
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
txtNombreContacto.setText(name); //print data
// column index of the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
txtTelefono.setText(phone); //print data
}
pCur.close();
// column index of the email
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
txtMailContacto.setText(email); //print data
}
emailCur.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Android contact picker that show email

just override @Override onActivityResult and create a cursor for email, when you get the results for picker, get only the email addreess.

email = Emailcursor.getString(Emailcursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

Incorrect Contact Number Fetching in Contact Picker - Android

I agree with @DaveyDaveDave static fields are not the way to go, but if you insist here's your same code, with lots of fixes and simplified code.
Note that NORMALIZED_NUMBER is only supported from API 16 and above.

class ClassGetPhoneNumber {

...

public static void retrieveContactNumber(Context context, Uri uriContact) {
sResponse = null;
sContactNumber = null;
sContactNumberFinal = null;

// get contactID from contactUri
long contactID = ContentUris.parseId(contactUri);

// Using the contact ID now we will get contact phone number
// Don't add selection by Phone.TYPE as we might miss a lot of phones.
// We ask for the normalized number, this will only work for API 16 and above
Cursor cursorPhone = context.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI,
new String[]{CommonDataKinds.Phone.NORMALIZED_NUMBER, CommonDataKinds.Phone.NUMBER},
CommonDataKinds.Phone.CONTACT_ID + " = " + contactID,null,null);

contactNumber = null;

if (cursorPhone != null && cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(0); // this number will always be of e164 format: "+<country><local number>"
Log.d(TAG, "normalized number: " + contactNumber + ", original number: " + cursorPhone.getString(1) + ", contact-id: " + contactID);
} else {
// you need to quit here, otherwise you'll be using an old value of contactNumber in the rest of the code.
if (cursorPhone != null) {
cursorPhone.close();
}
return;
}

cursorPhone.close();

if (!TextUtils.isEmpty(contactNumber)) {
sContactNumber = contactNumber;
sContactNumberFinal = sContactNumber.substring(3);
sResponse = sContactNumberFinal;
sStatus = "OK";
} else {
// no phone was found
sStatus = "NOT FOUND";
}
}
}


Related Topics



Leave a reply



Submit