I am trying to save all contacts telephone numbers in an ArrayList but I cant find a way how. Is there a way to get them instead of picking them one by one with ContactsContract?
            Asked
            
        
        
            Active
            
        
            Viewed 4.7k times
        
    29
            
            
        - 
                    what is ContactsContract – PSR Mar 06 '13 at 09:22
- 
                    This question is already answered in [This] [1] thread. Please check it out. [1]: http://stackoverflow.com/a/12562234/1773155 – Shajeel Afzal Mar 06 '13 at 09:34
- 
                    Will surely suites your requirement using [android-contact-extractor library](https://github.com/nitiwari-dev/android-contact-extractor) – Nitesh Tiwari May 12 '17 at 10:05
7 Answers
46
            
            
        ContentResolver cr = mContext.getContentResolver(); //Activity/Application android.content.Context
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if(cursor.moveToFirst())
    {
        ArrayList<String> alContacts = new ArrayList<String>();
        do
        {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            if(Integer.parseInt(cursor.getString(cursor.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 contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    alContacts.add(contactNumber);
                    break;
                }
                pCur.close();
            }
        } while (cursor.moveToNext()) ;
    }
- 
                    2contResv is an instance of ContentResolver, you can get by calling getContentResolver() method of class android.content.Context – Santhosh Apr 02 '13 at 04:43
- 
                    2Great, but I don't understand what's happening in the most inner while-loop. You always break it after one time. Seems to me you can use an IF there instead.. – Aviv Ben Shabat Dec 03 '15 at 08:57
- 
                    For a test with ~9000 contacts, this took ~99 seconds (MotoX, first gen). So pretty much useless.. You can use Lorem Contacts to generate some fake people and check https://play.google.com/store/apps/details?id=me.angrybyte.contactsgenerator – milosmns Oct 19 '16 at 16:57
- 
                    "Provide an explicit projection, to prevent reading data from storage that aren't going to be used. Passing null will return all columns, which is inefficient." (from docs) – Ahmet Noyan Kızıltan Feb 07 '18 at 22:24
- 
                    
- 
                    1Loop inside loop is inefficient, when you deal with many contacts try mudit's answer – Frildoren Oct 18 '19 at 10:22
19
            Try this:
Cursor managedCursor = getContentResolver()
    .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
     new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null,  Phone.DISPLAY_NAME + " ASC");
And by traversing through the cursor, you can store all this data in any data structure of your choice.
 
    
    
        mudit
        
- 25,306
- 32
- 90
- 132
- 
                    
- 
                    1I dont understand why the other answers with the double loops are found everywhere whereas this solution is so much cleaner ! – Christophe Blin Dec 11 '17 at 21:17
7
            
            
        This code will work much faster than code in the answer, because you don't make additional query for each contact.
private static final String CONTACT_ID = ContactsContract.Contacts._ID;
private static final String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
private static final String PHONE_NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
private static final String PHONE_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
public static ArrayList<String> getAll(Context context) {
    ContentResolver cr = context.getContentResolver();
    Cursor pCur = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[]{PHONE_NUMBER, PHONE_CONTACT_ID},
            null,
            null,
            null
    );
    if(pCur != null){
        if(pCur.getCount() > 0) {
            HashMap<Integer, ArrayList<String>> phones = new HashMap<>();
            while (pCur.moveToNext()) {
                Integer contactId = pCur.getInt(pCur.getColumnIndex(PHONE_CONTACT_ID));
                ArrayList<String> curPhones = new ArrayList<>();
                if (phones.containsKey(contactId)) {
                    curPhones = phones.get(contactId);
                }
                curPhones.add(pCur.getString(pCur.getColumnIndex(PHONE_NUMBER)));
                phones.put(contactId, curPhones);
            }
            Cursor cur = cr.query(
                    ContactsContract.Contacts.CONTENT_URI,
                    new String[]{CONTACT_ID, HAS_PHONE_NUMBER},
                    HAS_PHONE_NUMBER + " > 0",
                    null,null);
            if (cur != null) {
                if (cur.getCount() > 0) {
                    ArrayList<String> contacts = new ArrayList<>();
                    while (cur.moveToNext()) {
                        int id = cur.getInt(cur.getColumnIndex(CONTACT_ID));
                        if(phones.containsKey(id)) {
                            contacts.addAll(phones.get(id));
                        }
                    }
                    return contacts;
                }
                cur.close();
            }
        }
        pCur.close();
    }
    return null;
}
 
    
    
        Nikita Leshchev
        
- 1,784
- 2
- 14
- 26
- 
                    2Something not right here, you never use PHONE_NUMBER. I think you have a PHONE_CONTACT_ID where you should have PHONE_NUMBER – Greg Ennis Dec 20 '16 at 16:48
- 
                    @GregEnnis I can't check it now, but i think you're right. I'll edit my answer – Nikita Leshchev Dec 21 '16 at 14:06
1
            
            
        Try this also get all contacts.
Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, null , null , null,
                        "upper("+Phone.DISPLAY_NAME + ") ASC");
 
    
    
        Sandip Armal Patil
        
- 6,241
- 21
- 93
- 160
 
    
    
        srinivas tadinada
        
- 11
- 1
1
            
            
        in kotlin try this to get all contacts
fun getContacts(ctx: Context): List<ContactModel>? {
    val list: MutableList<ContactModel> = ArrayList()
    val contentResolver = ctx.contentResolver
    val cursor: Cursor? =
        contentResolver.query(
            ContactsContract.Contacts.CONTENT_URI, null,
            null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"
        )
    if (cursor!!.count > 0) {
        while (cursor.moveToNext()) {
            val id =
                cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
            if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                val cursorInfo: Cursor? = contentResolver.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    arrayOf(id),
                    null
                )
                val inputStream: InputStream? =
                    ContactsContract.Contacts.openContactPhotoInputStream(
                        ctx.contentResolver,
                        ContentUris.withAppendedId(
                            ContactsContract.Contacts.CONTENT_URI,
                            id.toLong()
                        )
                    )
                val person: Uri =
                    ContentUris.withAppendedId(
                        ContactsContract.Contacts.CONTENT_URI,
                        id.toLong()
                    )
                val pURI: Uri = Uri.withAppendedPath(
                    person,
                    ContactsContract.Contacts.Photo.CONTENT_DIRECTORY
                )
                var photo: Bitmap? = null
                if (inputStream != null) {
                    photo = BitmapFactory.decodeStream(inputStream)
                }
                while (cursorInfo!!.moveToNext()) {
                    val info = ContactModel()
                    info.setId(id)
                    info.setName(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)))
                    info.setMobileNumber(
                        cursorInfo.getString(
                            cursorInfo.getColumnIndex(
                                ContactsContract.CommonDataKinds.Phone.NUMBER
                            )
                        )
                    )
                    photo?.let { info.setPhoto(it) }
                    info.setPhotoURI(pURI)
                    list.add(info)
                }
                cursorInfo.close()
            }
        }
        cursor.close()
    }
    return list
}
Need to create one data class for this
class ContactModel {
@SerializedName("id")
private var id: String = ""
@SerializedName("name")
private var name: String? = ""
@SerializedName("mobileNumber")
private var mobileNumber: String? = ""
@SerializedName("photo")
private var photo: Bitmap? = null
@SerializedName("photoURI")
private var photoURI: Uri? = null
fun getId(): String {
    return id
}
fun setId(id: String) {
    this.id = id
}
fun getName(): String? {
    return name
}
fun setName(name: String) {
    this.name = name
}
fun getMobileNumber(): String? {
    return mobileNumber
}
fun setMobileNumber(mobileNumber: String) {
    this.mobileNumber = mobileNumber
}
fun getPhoto(): Bitmap? {
    return photo
}
fun setPhoto(photo: Bitmap) {
    this.photo = photo
}
fun getPhotoURI(): Uri? {
    return photoURI
}
fun setPhotoURI(photoURI: Uri) {
    this.photoURI = photoURI
}
override fun toString(): String {
    return "ContactModel(id='$id', name=$name, mobileNumber=$mobileNumber, photo=$photo, photoURI=$photoURI)"
}
}
 
    
    
        Abhay Pratap
        
- 1,886
- 11
- 15
0
            
            
        This method is optimized as well as fetch only distinct contacts
@RequiresApi(api = Build.VERSION_CODES.N)
private List<ModelContacts> getContacts() {
    ArrayList<ModelContacts> list = new ArrayList<>();
    Cursor cursor = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    cursor.moveToFirst();
    while (cursor.moveToNext()) {
        list.add(new ModelContacts(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                , cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))));
    }
    cursor.close();
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        List<ModelContacts> distinctList = list.stream().filter(distinctByKey(c -> c.getName()))
                .collect(Collectors.toList());
        
        return distinctList;
    }
    else {
       
        return list;
    }
}
@RequiresApi(api = Build.VERSION_CODES.N)
public static <T> Predicate<T> distinctByKey (final Function<? super T, Object> keyExtractor)
{
    Map<Object, Boolean> map = new ConcurrentHashMap<>();
    return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
 
    
    
        Areeb Momin
        
- 199
- 4
- 4
0
            
            
        Firstly, create a model class for storing Contacts Data: And add getAllContact(context) method in your contact: Don't forget to add user permision of Read Contacts in your manifest:
data class ContactModel(
    var name: String? = "",
    var mobileNumber: String? = "",
    var photoURI: Uri? = null
) 
class ContactsFragment : Fragment(R.layout.contacts_fragment) {
    private var _binding: ContactsFragmentBinding? = null
    private val binding get() = _binding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = ContactsFragmentBinding.inflate(inflater, container, false)
        return binding?.root
    }
    @SuppressLint("Recycle")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (!context?.let { checkIfAlreadyHavePermission(it) }!!) {
            context?.let { requestContactPermission(it) }
        } else {
            lifecycleScope.launch(Dispatchers.IO) {
                context?.let { getAllContacts(it) }
                Log.e("con", "con" + getAllContacts(requireContext()))
            }
        }
    }
fun getAllContacts(context: Context): List<ContactModel> {
        val contactList: ArrayList<ContactModel> = ArrayList()
        val contentResolver = context.contentResolver
        val notifier: Cursor? = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC")
        if (notifier!!.count > 0) {
            while (notifier.moveToNext()) {
                val id = notifier.getString(notifier.getColumnIndex(ContactsContract.Contacts._ID))
                if (notifier.getInt(notifier.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0)
                { val notifierInfo: Cursor? = contentResolver.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", arrayOf(id), null
                    )
                    val user: Uri =
                        ContentUris.withAppendedId(
                            ContactsContract.Contacts.CONTENT_URI,
                            id.toLong()
                        )
                    val userURI: Uri = Uri.withAppendedPath(
                        user,
                        ContactsContract.Contacts.Photo.CONTENT_DIRECTORY
                    )
                    while (notifierInfo!!.moveToNext()) {
                        val info = ContactModel()
                        info.name =
                            (notifier.getString(notifier.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)))
                        info.mobileNumber = (
                                notifierInfo.getString(
                                    notifierInfo.getColumnIndex(
                                        ContactsContract.CommonDataKinds.Phone.NUMBER
                                    )
                                )
                                )
                        contactList.add(info)
                    }
                    notifierInfo.close()
                }
            }
            notifier.close()
        }
        return contactList
    }
 
 
    
    
        shivam rana
        
- 1
- 3
 
     
     
    