get Email Address from contact list

后端 未结 3 541
有刺的猬
有刺的猬 2020-12-17 05:33

I getting contact list by

permission

android:name=\"android.permission.READ_CONTACTS\"


Intent intent = new Intent(Intent.ACTION_PICK, ContactsCon         


        
相关标签:
3条回答
  • 2020-12-17 06:06

    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.
    }
    
    0 讨论(0)
  • 2020-12-17 06:07

    function for fetch email id of selected contact

    private void retrieveContactEmail()
    {
        Cursor cursorID = getContentResolver().query(uriContact,
            new String[]{ContactsContract.Contacts._ID},
            null, null, null);
    
        if (cursorID.moveToFirst()) {
            contactID = cursorID.getString(
                cursorID.getColumnIndex(ContactsContract.Contacts._ID));
        }
    
        Cursor cursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, 
            ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?",
            new String[]{contactID}, null);
    
        int emailIdx = cursor.getColumnIndex(
            ContactsContract.CommonDataKinds.Email.DATA);
    
        if (cursor.moveToFirst()) {
            String emailg = cursor.getString(emailIdx);
    
            if(emailg!=null) {
                email.setText(emailg);
            }
            else {
                Toast.makeText(Activity.this,
                    "No email id for this contact",
                    Toast.LENGTH_LONG
                ).show();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-17 06:28

    use

    String emailAdd =  cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
    

    instead of

    String emailAdd =  cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
    
    0 讨论(0)
提交回复
热议问题