Please, I've been trying all day to get the email address of all the contact on a phone but somehow, i can't get it to work. Please can someone tell me where am going wrong.. all others work except getting the email. Thanks in advance
Cursor cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
//now we have cusror with contacts and get diffrent value from cusror.
while (cursor.moveToNext()) {
String id = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
contactView.append("Name: ");
contactView.append(name);
contactView.append("\n");
if (Integer
.parseInt(cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactView.append("Number: ");
contactView.append(phoneNumber);
contactView.append("\n");}
//get email cursor
Cursor ecursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "+id,
null, null);
while (ecursor.moveToNext()) {
String email = ecursor.getString(ecursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if(email != null){
contactView.append("email: ");
contactView.append(email);
contactView.append("\n");
}
}
ecursor.close();
}
cursor.close();
Related
How can I get a list of contacts that either have a phone number or an email in Android.
I'm a bit confused with the contracts and how can I join the info.
Thank you
Here is how I fetched contacts with email and phone number. The Contact object is just a simple pojo that I created. This code is in an AsyncTask that I run after the user has provided permission to access Contacts.
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
// get the contact's information
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Integer hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
// get the user's email address
String email = null;
Cursor ce = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
if (ce != null && ce.moveToFirst()) {
email = ce.getString(ce.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
ce.close();
}
// get the user's phone number
String phone = null;
if (hasPhone > 0) {
Cursor cp = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
if (cp != null && cp.moveToFirst()) {
phone = cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
cp.close();
}
}
// if the user user has an email or phone then add it to contacts
if ((!TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
&& !email.equalsIgnoreCase(name)) || (!TextUtils.isEmpty(phone))) {
Contact contact = new Contact();
contact.name = name;
contact.email = email;
contact.phoneNumber = phone;
contacts.add(contact);
}
} while (cursor.moveToNext());
// clean up cursor
cursor.close();
}
For DISPLAY_NAME you can use the following:
private final String DISPLAY_NAME = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME;
Here is a link to an AsyncTask Example that I use with this.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
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()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
}
pCur.close();
}
}
}
Here is the code used to get the contact information :
String id = data.getData().getLastPathSegment();
Cursor cursor = getActivity().getContentResolver()
.query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.Data.DISPLAY_NAME},
ContactsContract.Data.CONTACT_ID + "=?",
new String[]{id},
null);
// short circuit if we didn't pick a contact
if (cursor.getCount() == 0) {
return;
}
String contact = "";
String contactName = "";
//code to get contact name
if (cursor.moveToFirst() && cursor.getString(0) != null) {
contact = contact + cursor.getString(0) + ",";
contactName = cursor.getString(0);
}
else {
contact = contact + ","; //changed
}
cursor.close();
// code to get phone number
cursor = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
newString[{ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[]{id},null);
if (cursor.moveToFirst() && cursor.getString(0) != null) {
contact = contact + cursor.getString(0) + ",";
contact = contact + cursor.getString(0) + ",";
}
else {
contact = contact + ",,"; //changed
}
cursor.close();
//Code to get email
cursor = getActivity().getContentResolver()
.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS},
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?",
new String[]{id},null);
if (cursor.moveToFirst() && cursor.getString(0) != null) {
contact = contact + cursor.getString(0);
}
cursor.close();
contact = contact.replace("+", "");
Log.i("TAG", "CONTACT INFO = " + contact);
What I am trying to do:
Here, I query the contact name, phone number and email from Contacts through content provider and concatenate all three into a single string, with commas separating them within the string .
The problem :
I get the contact name and email, without any issues.
However, when I select some contacts from contact, the phone number returned is empty.
The Contacts that return empty phone numbers are phone numbers from a different country, from my current location.
I am right now in Canada and when I select Canadian Contacts, I get the phone number correctly.
However, when I select phone numbers from India, the number is not returned, instead, I get empty result.
The same happens when I select a Contact with numbers like "12345" or "5555555555"etc.
My Question
Could it be that Android is trying to validate the authenticity of the number and returning an empty number, if it finds the phone number Invalid? *(probably not! Must be my mistake!)*
How can I solve this issue? Can't I get the value of the phone number just like how it is saved in the Contacts by the user?
I apologize for any unambiguity in the code or my question. I am a novice programmer and thanks a lot for all your inputs!
One thing i observed from that code is, you are reading contacts with out using
ContactsContract.Contacts.CONTENT_URI
I Modified your code and created a new method to print all contacts.
public static void fetchContacts(Context context) {
String phoneNumber = null;
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
StringBuffer output = new StringBuffer();
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
long hasPhoneNumber = Long.parseLong(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
if (hasPhoneNumber > 0) {
output.append("\n Name:" + name);
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);
while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
output.append("\n Number:" + phoneNumber);
System.out.println("Number:::::"+phoneNumber);
System.out.println("Contact:::::"+output.toString());
}
phoneCursor.close();
}
}
}else{
// Toast.makeText(context, "No contacts Found", Toast.LENGTH_LONG).show();
}
}
I want to get Contact Image, Name, Number from my mobile using android. im using ContactsContract to fetch these information. but i can't. can anyone know the exact tutorial for learning. i want to list these info in a custom listview. Thanks in Advance. i hope your valuable guiding will help me.
PS: In the DB i have my friends number. i want to synchronize the phone contact and the DB contact using the matched phone no. and i need to display all the matched contacts in the listview. the listview have imageView, name, number..
setReminder.numbers.clear();
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
((Activity) mcontext).startActivityForResult(intent, 1);
and on activity result do this
if (requestCode == 1 && resultCode == Activity.RESULT_OK)
{
getContactInfo(intent);
}
}
protected void getContactInfo(Intent intent)
{
String phoneNumber = "";
//String email="";
Cursor cursor = ((Activity) mcontext).managedQuery(intent.getData(), null, null, null, null);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(BaseColumns._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
//Cursor Email = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
numbers.add(phoneNumber);
}
phones.close();
Cursor emailCur = ((Activity) mcontext).getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId,
null, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
if(email==null)
{
email="";
}
}
}
if(phoneNumber != null && phoneNumber.length() > 0){
chooseContactArray.clear();
for(int i=0;i<numbers.size();i++)
{
chooseContactArray.add(numbers.get(i));
}
adapter.notifyDataSetChanged();
}
cursor.close();
}
I have a listview of all the contact names in the phone. I want to get the email id (if contact have one) of the contact which I click on in the listview. How can I do this?
Use the following code to get all email ids. I checked the code. It is working.
public static void getContactEmails(Context context) {
String emailIdOfContact = null;
int emailType = Email.TYPE_WORK;
String contactName = null;
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(BaseColumns._ID));
contactName = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Log.i(TAG,"....contact name....." +
// contactName);
cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
Cursor emails = cr.query(Email.CONTENT_URI, null,
Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext()) {
emailIdOfContact = emails.getString(emails
.getColumnIndex(Email.DATA));
// Log.i(TAG,"...COntact Name ...."
// + contactName + "...contact Number..."
// + emailIdOfContact);
emailType = emails.getInt(emails
.getColumnIndex(Phone.TYPE));
}
emails.close();
}
}// end of contact name cursor
cur.close();
}
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();
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,Uri.encode(name.toString().trim()));
Cursor mapContact = getContext().getContentResolver().query(uri, new String[]{PhoneLookup._ID}, null, null, null);
if(mapContact.moveToNext())
{
String _id = mapContact.getString(mapContact.getColumnIndex(ContactsContract.Contacts._ID));
}
Xamarin Version of Sunil's answer. Took me a while, but I figured it out.
ContentResolver cr = ContentResolver;
string contactName = null;
var cur = cr.Query(ContactsContract.Contacts.ContentUri,null,null,null,null);
if (cur.MoveToFirst())
{
do
{
string id = cur.GetString(cur.GetColumnIndex(BaseColumns.Id));
contactName = cur.GetString(cur.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.DisplayName));
var emails = cr.Query(ContactsContract.CommonDataKinds.Email.ContentUri, null, ContactsContract.CommonDataKinds.Email.InterfaceConsts.ContactId + " = " + id, null, null);
if (emails.MoveToFirst()) {
do
{
// This is where it loops through if there are multiple Email addresses
var email = emails.GetString(emails.GetColumnIndex(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data));
} while (emails.MoveToNext());
}
} while (cur.MoveToNext());
}
I am using below code. it is working fine. checked it.
ArrayList<ContactInfo> listContactsData = new ArrayList<>();
// Retrieve Email address
Cursor emailCursor = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
// This would allow you get email addresses
String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); String emailType = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Log.e(“Email :“,” ”+email)
objContact.strEmail = email;
}
emailCur.close();
listContactsData.add(objContact);
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String email= "";
if (Integer.valueOf(hasNumber) == 1) {
Cursor numbers = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (numbers.moveToNext()) {
email= numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
//Toast.makeText(getApplicationContext(), "Number=" + num, Toast.LENGTH_LONG).show();
//asdasdasdsa
if(getEmail(email).isEmpty()){
Toast.makeText(this, "Email Not Found In That Contact Try Another", Toast.LENGTH_SHORT).show();
}
else {
edt_email_contact.setText("" + getEmail(email));
} }
}
}
break;
}
Hi
I am developing a contact based application in that i want get the email id from the contacts to my application
I am developing the application for android 1.6
Please any on help me to do that.Thanks in advance
You can refer to this link http://thinkandroid.wordpress.com/2010/01/19/retrieving-contact-information-name-number-and-profile-picture/
find the line String[] columns = new String[] { People.NAME, People.NUMBER };
and you can use this http://developer.android.com/reference/android/provider/Contacts.People.html#PRIMARY_EMAIL_ID
to get email id. I am not sure on this but yes you can give it a try
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
int index = 0;
if (cur.getCount() > 0) {
emailNames = new String[cur.getCount()];
emailNumbers = new String[cur.getCount()];
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
emailNames[index] = name;
Cursor emails = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + id, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails
.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.v("email==>", emailAddress);
emailNumbers[index] = emailAddress;
}
}
emails.close();
index++;
}
cur.close();