androidandroid-contacts

Android : Not getting any contacts from contact list


I am fetching contacts successfully by

 val cursor = context.contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null)

but the problem occurs when I don't have any contacts in my contact list and I adding contacts in google account from web end, after I sync contact in my phone from this xxx@gmail account, All contacts display in my contact list but when I am trying to fetch contact then, It returns 0.

I don't know what is the problem, there is any other way to fetch this contacts!

If I insert new contact than I got this contact from above query, but can't find those contacts which I have added in web panel using google account.

Help me if you face this type of problem.

I am using this following code to fetch contacts

val cursor = context.contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null)
    val projection = arrayOf(ContactsContract.Contacts._ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.PHOTO_URI, ContactsContract.CommonDataKinds.Phone.TYPE)
    val alContacts = ArrayList<Contact>()
    var mMapContact = JSONObject()
    if (cursor!!.moveToFirst()) {
        do {
            val id = cursor!!.getString(cursor!!.getColumnIndex(ContactsContract.Contacts._ID))
            if (Integer.parseInt(cursor!!.getString(cursor!!.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                val pCur = context.contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", arrayOf<String>(id), ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC")
                var mListMobile = ArrayList<MobileType>()
                var mNameContact = ""
                while (pCur!!.moveToNext()) {
                    val number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                    mNameContact = pCur!!.getString(pCur!!.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
                    val type = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))
                    when (type) {
                        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE -> {
                            mListMobile.add(MobileType(number, 0))
                            mMapContact.put(number.replace(Regex(CONTACT_PLUS_PATH_PREFIX), ""), mNameContact)
                        }
                        ContactsContract.CommonDataKinds.Phone.TYPE_HOME -> {
                            mListMobile.add(MobileType(number, 1))
                            mMapContact.put(number.replace(Regex(CONTACT_PLUS_PATH_PREFIX), ""), mNameContact)
                        }
                        ContactsContract.CommonDataKinds.Phone.TYPE_WORK -> {
                            mListMobile.add(MobileType(number, 2))
                            mMapContact.put(number.replace(Regex(CONTACT_PLUS_PATH_PREFIX), ""), mNameContact)
                        }
                    }
                    /*val contactNumber = pCur!!.getString(pCur!!.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                    val contactName = pCur!!.getString(pCur!!.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
                    val contactInfo = ContactInfo(""+id, ""+contactName, "", ""+contactNumber)
                    LogUtil.e(TAG, "getContactList: $contactNumber - $contactName - $id")
                    alContacts.add(contactInfo)
                    break*/
                }
                pCur?.close()
                if(mListMobile.size > 0) {
                    var contact : Contact? = null
                    for (mobile in mListMobile) {
                        contact = Contact(id, "" + mNameContact, ""+mNameContact, false, null, ""+mobile.mobile, "", mobile.type)
                        alContacts.add(contact!!)
                    }
                }
            }
        } while (cursor?.moveToNext())
    }

Solution

  • You can use like this following:

    String[] projection = {
                            ContactsContract.Data.MIMETYPE,
                            ContactsContract.Data.CONTACT_ID,
                            ContactsContract.Contacts.DISPLAY_NAME,
                            ContactsContract.Contacts.PHOTO_URI,
                            ContactsContract.Contacts.STARRED,
                            ContactsContract.RawContacts.ACCOUNT_TYPE,
                            ContactsContract.CommonDataKinds.Contactables.DATA,
                            ContactsContract.CommonDataKinds.Contactables.TYPE
                    };
    
    
    
                    String selection = ContactsContract.Data.MIMETYPE + " in (?, ?)" + " AND " +
                            ContactsContract.Data.HAS_PHONE_NUMBER + " = '" + 1 + "'";
    
                    String[] selectionArgs =
                            {
                                    ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
                                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
                            };
                    String sortOrder = ContactsContract.Contacts.SORT_KEY_ALTERNATIVE;
    
    
    
                    Uri uri = null;
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
                        uri = ContactsContract.CommonDataKinds.Contactables.CONTENT_URI;
                    }
                    else
                    {
                        uri = ContactsContract.Data.CONTENT_URI;
    
                    }
    
    
                    Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
    
                    final int mimeTypeIdx = cursor.getColumnIndex(ContactsContract.Contacts.Data.MIMETYPE);
                    final int idIdx = cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID);
                    final int nameIdx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                    Log.e("Contacts----", "ontacts__nameIdx----" + nameIdx);
                    final int dataIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.DATA);
                    final int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.TYPE);
                    final int photo = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.PHOTO_URI);
                    final int account_type = cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE);
                    sb = new StringBuilder();
                    while (cursor.moveToNext()) {
                        long id = cursor.getLong(idIdx);
    
                        int type = cursor.getInt(typeIdx);
                        String data = cursor.getString(dataIdx);
                        String mimeType = cursor.getString(mimeTypeIdx);
                        String photo_uri = cursor.getString(photo);
                        String account_t = cursor.getString(account_type);
    
                        Log.e("Contacts----", "Contacts__----" + nameIdx+"---"+type+"---"+data+"----"+id);
                        if (mimeType.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
    
                            /**For email  **/
                            Log.e("Contacts----","Contacts__>>"+type+"---data---"+data+"--id--"+id);
    
                        } else {
                            //
                            Log.e("Contacts-----","Contacts__else>>"+type+"---data---"+data+"--id--"+id);
    
                        }
                    }
    
                    cursor.close();
    

    Hope It'll help you.