Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

android - get Email Address from contact list

I getting contact list by

permission

android:name="android.permission.READ_CONTACTS"


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

but how to get Email address from

 public void onActivityResult(int reqCode, int resultCode, Intent data) {
//what should i have to write to fetch email address of selected contact
// I wrote like below but i could not get result

 if (resultCode == Activity.RESULT_OK) {  
     try{
             Uri contactData = data.getData();

             Cursor cursorEmail = getContentResolver().query(contactData,null,null,null,null);
             cursorEmail.moveToFirst();
             String emailAdd =  cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
             Toast.makeText(MySettings.this, emailAdd, Toast.LENGTH_LONG).show();
          }catch(Exception e){
              Toast.makeText(MySettings.this, "No Email Add found", Toast.LENGTH_LONG).show();
          }

}

but the problem is that i am not getting Email address from selected contact list so can any one give me solution

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use following code to retrieve email.

public ArrayList<String> ShowContact() {        

    nameList = new ArrayList<String>();
            phoneList = new ArrayList<String>();
            emailList = 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));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                // Query phone here. Covered next

                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
                    String phoneNo = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    nameList.add(name); // Here you can list of contact.
                        phoneList.add(phoneNo); // Here you will get list of phone number.                  


                    Cursor emailCur = cr.query( 
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                            null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                            new String[]{id}, null); 
                        while (emailCur.moveToNext()) { 
        String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));                            

                 emailList.add(email); // Here you will get list of email    

                        } 
                        emailCur.close();       
                }
                pCur.close();
            }
        }
    }

    return nameList; // here you can return whatever you want.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...