Access contacts and get email address

android, contacts, email

Solution

make sure your app has got permission, otherwise you'll get weird Exceptions

in AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_CONTACTS" />

Problem

I have a code snippet for accessing to Contacts. When user click the button then the contacts list will be open and user can choose a person from contacts and the person's email address should be write on a edittext. I can receive the email from the persons which user select. But I can't set it to the edittext. ``` static String email = ""; imgbtnaddfromcontacts.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (v == imgbtnaddfromcontacts) { try { Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, 1); } catch (Exception e) { e.printStackTrace(); Log.e("Error in intent : ", e.toString()); } } } }); kimeTxt.setText(email); } @Override public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); try { if (resultCode == Activity.RESULT_OK) { // Get data Uri contactData = data.getData(); // Cursor Cursor cur = managedQuery(contactData, null, null, null, null); ContentResolver contect_resolver = getContentResolver(); // List if (cur.moveToFirst()) { String id = cur .getString(cur .getColumnIndexOrThrow(ContactsContract.Contacts._ID)); Cursor phoneCur = contect_resolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); Cursor emailCur = contect_resolver.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null); if (phoneCur.moveToFirst()) { name = phoneCur .getString(phoneCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); no = phoneCur .getString(phoneCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } 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)); if (email != null) { seciliEmail = email; } else { Toast.makeText(EpostaIletActivity.this, "Kişinin eposta hesabı bulunmamaktadır.", Toast.LENGTH_SHORT); Log.w("Error: ", "Kişinin eposta hesabı yok."); } } Log.e("Phone no & name & email :***: ", name + " : " + no + ":" + email); // txt.append(name + " : " + no + "\n"); id = null; name = null; no = null; seciliEmail = "xxx"; phoneCur = null; emailCur.close(); } contect_resolver = null; cur = null; // populateContacts(); } } catch (IllegalArgumentException e) { e.printStackTrace(); Log.e("IllegalArgumentException :: ", e.toString()); } catch (Exception e) { e.printStackTrace(); Log.e("Error :: ", e.toString()); } } ```

Original source