android - How to fetch the E Mail ID of A contact -
i trying fetch e mail address of contact. found how contact email id? not use this.
i have written following code fetch email
cursor c1; c1 = getcontentresolver().query(contactscontract.contacts.content_uri, null, null, null, contactscontract.contacts.display_name); if(c1==null) return; if(c1.getcount() > 0) { while(c1.movetonext()) { string id = c1.getstring(c1.getcolumnindex(contacts._id)); if(id==null) continue; cursor email_crsr = getcontentresolver().query(commondatakinds.email.content_uri, null, commondatakinds.phone.contact_id +" = ?", new string[]{id}, null); if(email_crsr!=null) email = phone_crsr.getstring(phone_crsr.getcolumnindex(commondatakinds.email.data)); } }
i following exception : 04-09 10:38:59.487: e/androidruntime(8150): caused by: android.database.cursorindexoutofboundsexception: index -1 requested, size of 4
this may helps
public arraylist<string> getnameemaildetails(){ arraylist<string> names = new arraylist<string>(); 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)); cursor cur1 = cr.query( contactscontract.commondatakinds.email.content_uri, null, contactscontract.commondatakinds.email.contact_id + " = ?", new string[]{id}, null); while (cur1.movetonext()) { //to contact names string name=cur1.getstring(cur1.getcolumnindex(contactscontract.commondatakinds.phone.display_name)); // log.e("name :", name); string email = cur1.getstring(cur1.getcolumnindex(contactscontract.commondatakinds.email.data)); // log.e("email", email); if(email!=null){ names.add(email); } } cur1.close(); } } return names; }
Comments
Post a Comment