Not able to get email address from picked contact in android -
i trying pick contact using intent in android. information need name, phone no , email address of contact. following have tried far.
@override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == activity.result_ok) { // check request code, might usign multiple startactivityforreslut switch (requestcode) { case result_pick_contact: contactpicked(data); break; } } } private void contactpicked(intent data) { cursor cursor = null; try { // getdata() method have content uri of selected contact uri uri = data.getdata(); //query content uri cursor = getactivity().getcontentresolver().query(uri, null, null, null, null); cursor.movetofirst(); int phoneindex = cursor.getcolumnindex(contactscontract.commondatakinds.phone.number); int nameindex = cursor.getcolumnindex(contactscontract.commondatakinds.phone.display_name); string phoneno = cursor.getstring(phoneindex); string name = cursor.getstring(nameindex); string contactbookid = uri.getlastpathsegment(); string email = null; try { cursor emailcur = getactivity().getcontentresolver().query(contactscontract.commondatakinds.email.content_uri, null, contactscontract.commondatakinds.email.contact_id + " = ?", new string[]{contactbookid}, null); emailcur.movetofirst(); while (emailcur.movetonext()) { string phone = emailcur.getstring(emailcur.getcolumnindex(contactscontract.commondatakinds.email.data)); int type = emailcur.getint(emailcur.getcolumnindex(contactscontract.commondatakinds.email.type)); string s = (string) contactscontract.commondatakinds.email.gettypelabel(getactivity().getresources(), type, ""); log.d("tag", s + " email: " + phone); } emailcur.close(); } catch (exception e) { log.e(tag, "exception while trying fetch email addresses of " + name + ":" + e.getmessage()); } cursor.close(); } catch (exception e) { e.printstacktrace(); } }
here how firing intent
intent contactpickerintent = new intent(intent.action_pick, contactscontract.commondatakinds.phone.content_uri); startactivityforresult(contactpickerintent, result_pick_contact);
my problem emailcur never reads email address. code never enters in loop.
edit
even if ditch getlastpathsegment()
, retrieve id follows same behaviour
int idindex = cursor.getcolumnindex(contactscontract.data.contact_id); string contactbookid = cursor.getstring(idindex);
the problem here:
emailcur.movetofirst(); while (emailcur.movetonext()) { ... }
you moving cursor 2 times before reading, , if contact has 1 email never enters in while loop.
delete emailcur.movetofirst();
Comments
Post a Comment