Android get all contacts

How can I get all the contact names in my Android and put them in an array of strings?

+75
android
Sep 24 '12 at 9:23
source share
8 answers

Try also

private void getContactList() { ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if ((cur != null ? cur.getCount() : 0) > 0) { while (cur != null && cur.moveToNext()) { String id = cur.getString( cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); while (pCur.moveToNext()) { String phoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); Log.i(TAG, "Name: " + name); Log.i(TAG, "Phone Number: " + phoneNo); } pCur.close(); } } } if(cur!=null){ cur.close(); } } 

If you need more links, refer to this link Read ContactList

+151
Sep 24 '12 at 9:27
source share

get a contact list in just a few lines of code

 Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); while (phones.moveToNext()) { String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Toast.makeText(getApplicationContext(),name, Toast.LENGTH_LONG).show(); } phones.close(); 
+45
Jan 12 '15 at 2:42
source share
 public class MyActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> { private static final int CONTACTS_LOADER_ID = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Prepare the loader. Either re-connect with an existing one, // or start a new one. getLoaderManager().initLoader(CONTACTS_LOADER_ID, null, this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { // This is called when a new Loader needs to be created. if (id == CONTACTS_LOADER_ID) { return contactsLoader(); } return null; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { //The framework will take care of closing the // old cursor once we return. List<String> contacts = contactsFromCursor(cursor); } @Override public void onLoaderReset(Loader<Cursor> loader) { // This is called when the last Cursor provided to onLoadFinished() // above is about to be closed. We need to make sure we are no // longer using it. } private Loader<Cursor> contactsLoader() { Uri contactsUri = ContactsContract.Contacts.CONTENT_URI; // The content URI of the phone contacts String[] projection = { // The columns to return for each row ContactsContract.Contacts.DISPLAY_NAME } ; String selection = null; //Selection criteria String[] selectionArgs = {}; //Selection criteria String sortOrder = null; //The sort order for the returned rows return new CursorLoader( getApplicationContext(), contactsUri, projection, selection, selectionArgs, sortOrder); } private List<String> contactsFromCursor(Cursor cursor) { List<String> contacts = new ArrayList<String>(); if (cursor.getCount() > 0) { cursor.moveToFirst(); do { String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); contacts.add(name); } while (cursor.moveToNext()); } return contacts; } } 

and do not forget

 <uses-permission android:name="android.permission.READ_CONTACTS" /> 
+11
Aug 6 '14 at 10:09 on
source share

Get contact information, photo contacts, photo URI and convert to class model

one). Sample for a class model:

  public class ContactModel { public String id; public String name; public String mobileNumber; public Bitmap photo; public Uri photoURI; } 

2). get contacts and convert to model

  public List<ContactModel> getContacts(Context ctx) { List<ContactModel> list = new ArrayList<>(); ContentResolver contentResolver = ctx.getContentResolver(); Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cursor.getCount() > 0) { while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { Cursor cursorInfo = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(ctx.getContentResolver(), ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id))); Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)); Uri pURI = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); Bitmap photo = null; if (inputStream != null) { photo = BitmapFactory.decodeStream(inputStream); } while (cursorInfo.moveToNext()) { ContactModel info = new ContactModel(); info.id = id; info.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); info.mobileNumber = cursorInfo.getString(cursorInfo.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); info.photo = photo; info.photoURI= pURI; list.add(info); } cursorInfo.close(); } } cursor.close(); } return list; } 
+11
Jan 24 '17 at 11:27
source share
 Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); String aNameFromContacts[] = new String[contacts.getCount()]; String aNumberFromContacts[] = new String[contacts.getCount()]; int i = 0; int nameFieldColumnIndex = contacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); int numberFieldColumnIndex = contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); while(contacts.moveToNext()) { String contactName = contacts.getString(nameFieldColumnIndex); aNameFromContacts[i] = contactName ; String number = contacts.getString(numberFieldColumnIndex); aNumberFromContacts[i] = number ; i++; } contacts.close(); 

The result will be an array of names filled with contacts. Also make sure you add

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

in main.xml

+3
Sep 24 '12 at 9:40
source share

This is the method to get the contact list. Name and number

  private void getAllContacts() { ContentResolver contentResolver = getContentResolver(); Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); if (cursor.getCount() > 0) { while (cursor.moveToNext()) { int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); if (hasPhoneNumber > 0) { String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); Cursor phoneCursor = contentResolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); if (phoneCursor != null) { if (phoneCursor.moveToNext()) { String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); //At here You can add phoneNUmber and Name to you listView ,ModelClass,Recyclerview phoneCursor.close(); } } } } } } 
+1
May 17 '18 at 10:02
source share
 //GET CONTACTLIST WITH ALL FIELD... public ArrayList < ContactItem > getReadContacts() { ArrayList < ContactItem > contactList = new ArrayList < > (); ContentResolver cr = getContentResolver(); Cursor mainCursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (mainCursor != null) { while (mainCursor.moveToNext()) { ContactItem contactItem = new ContactItem(); String id = mainCursor.getString(mainCursor.getColumnIndex(ContactsContract.Contacts._ID)); String displayName = mainCursor.getString(mainCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id)); Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO); //ADD NAME AND CONTACT PHOTO DATA... contactItem.setDisplayName(displayName); contactItem.setPhotoUrl(displayPhotoUri.toString()); if (Integer.parseInt(mainCursor.getString(mainCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { //ADD PHONE DATA... ArrayList < PhoneContact > arrayListPhone = new ArrayList < > (); Cursor phoneCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); if (phoneCursor != null) { while (phoneCursor.moveToNext()) { PhoneContact phoneContact = new PhoneContact(); String phone = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); phoneContact.setPhone(phone); arrayListPhone.add(phoneContact); } } if (phoneCursor != null) { phoneCursor.close(); } contactItem.setArrayListPhone(arrayListPhone); //ADD E-MAIL DATA... ArrayList < EmailContact > arrayListEmail = new ArrayList < > (); Cursor emailCursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null); if (emailCursor != null) { while (emailCursor.moveToNext()) { EmailContact emailContact = new EmailContact(); String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); emailContact.setEmail(email); arrayListEmail.add(emailContact); } } if (emailCursor != null) { emailCursor.close(); } contactItem.setArrayListEmail(arrayListEmail); //ADD ADDRESS DATA... ArrayList < PostalAddress > arrayListAddress = new ArrayList < > (); Cursor addrCursor = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = ?", new String[] { id }, null); if (addrCursor != null) { while (addrCursor.moveToNext()) { PostalAddress postalAddress = new PostalAddress(); String city = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)); String state = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)); String country = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)); postalAddress.setCity(city); postalAddress.setState(state); postalAddress.setCountry(country); arrayListAddress.add(postalAddress); } } if (addrCursor != null) { addrCursor.close(); } contactItem.setArrayListAddress(arrayListAddress); } contactList.add(contactItem); } } if (mainCursor != null) { mainCursor.close(); } return contactList; } //MODEL... public class ContactItem { private String displayName; private String photoUrl; private ArrayList<PhoneContact> arrayListPhone = new ArrayList<>(); private ArrayList<EmailContact> arrayListEmail = new ArrayList<>(); private ArrayList<PostalAddress> arrayListAddress = new ArrayList<>(); public String getDisplayName() { return displayName; } public void setDisplayName(String displayName) { this.displayName = displayName; } public String getPhotoUrl() { return photoUrl; } public void setPhotoUrl(String photoUrl) { this.photoUrl = photoUrl; } public ArrayList<PhoneContact> getArrayListPhone() { return arrayListPhone; } public void setArrayListPhone(ArrayList<PhoneContact> arrayListPhone) { this.arrayListPhone = arrayListPhone; } public ArrayList<EmailContact> getArrayListEmail() { return arrayListEmail; } public void setArrayListEmail(ArrayList<EmailContact> arrayListEmail) { this.arrayListEmail = arrayListEmail; } public ArrayList<PostalAddress> getArrayListAddress() { return arrayListAddress; } public void setArrayListAddress(ArrayList<PostalAddress> arrayListAddress) { this.arrayListAddress = arrayListAddress; } } public class EmailContact { private String email = ""; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } public class PhoneContact { private String phone=""; public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } } public class PostalAddress { private String city=""; private String state=""; private String country=""; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } } 
+1
Aug 28 '18 at 5:02
source share

Improving @Adiii's answer - this will clear the phone number and remove all duplicates

Declare a global variable

 // Hash Maps Map<String, String> namePhoneMap = new HashMap<String, String>(); 

Then use the function below

 private void getPhoneNumbers() { Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); // Loop Through All The Numbers while (phones.moveToNext()) { String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // Cleanup the phone number phoneNumber = phoneNumber.replaceAll("[()\\s-]+", ""); // Enter Into Hash Map namePhoneMap.put(phoneNumber, name); } // Get The Contents of Hash Map in Log for (Map.Entry<String, String> entry : namePhoneMap.entrySet()) { String key = entry.getKey(); Log.d(TAG, "Phone :" + key); String value = entry.getValue(); Log.d(TAG, "Name :" + value); } phones.close(); } 

Remember that in the above example, the key is the phone number and the value is the name, so read the contents, for example, 998xxxxx282-> Mahatma Gandhi instead of Mahatma Gandhi-> 998xxxxx282

0
Jan 17 '19 at 2:37
source share



All Articles