0

I want to fetch all contact data including all raw contacts. I referred this link

and this is the code that i am using :

Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,    Long.valueOf(raw_contact_id));
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);

Cursor c = getContentResolver().query(entityUri,
        new String[] { RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1,
                Entity.DATA2, Entity.DATA3, Entity.DATA4, Entity.DATA5, Entity.DATA6, Entity.DATA7,
                Entity.DATA8, Entity.DATA9, Entity.DATA10, Entity.DATA11, Entity.DATA12, Entity.DATA13,
                Entity.DATA14/* , Entity.DATA15 */ },
        null, null, null);

try {
    while (c.moveToNext()) {
        String sourceId = c.getString(0);
        if (!c.isNull(1)) {
            String mimeType = c.getString(2);
            // if (!mimeType.contains("photo")) {
            String data1 = c.getString(3);
            String data2 = c.getString(4);
            String data3 = c.getString(5);
            String data4 = c.getString(6);
        }
    }
} finally {
    c.close();
}

The problem is that the implementation of this code takes a lot of time to fetch all contacts-data.

Is there any other way to get the contacts-data in under 5 seconds.

Harish Ved
  • 560
  • 4
  • 17
Manvendra Sah
  • 103
  • 1
  • 10
  • 1
    what kind of data do you want to get? – pskink Dec 28 '15 at 08:10
  • Why you need to get all data in once time. Just get data when you needed. – Tran Vinh Quang Dec 28 '15 at 08:11
  • don't get data from all columns if not required – Vivek Mishra Dec 28 '15 at 08:17
  • I need phone numbers, emails, organisation, events, relationship, groups, addresses, structured name, etc for all the raw contacts which form a particular contact.. Need it for a work project.. Kindly share some thoughts. – Manvendra Sah Dec 28 '15 at 08:18
  • see [this](http://stackoverflow.com/a/26820544/2252830) and add your additional data (now it gets name, emails & phones), btw how many contacts do you have? – pskink Dec 28 '15 at 08:20
  • 1
    the speed will depend on the amount of contact data you have. 1000s of contacts will easily exceed your 5s requirement. why not do the query asynchronously and only show the results only after all data is loaded. – Angel Koh Dec 28 '15 at 08:48
  • Some phones may have 1000s of contacts, except for those cases, lets say a phone has 500 contacts.. Is there a way to get those contacts with all the data in under 5 seconds.. That what i want to know. – Manvendra Sah Dec 28 '15 at 09:14
  • the OP of the link i posted says it takes ~2 sec for 700 contacts – pskink Dec 28 '15 at 09:30
  • @psklink : i am able to get all the data that is required. Problem is the time taken . It taking about 20s to get all data for 100 contacts. And i am not able to optimise this. Kindly help me.. – Manvendra Sah Dec 28 '15 at 09:31
  • the OP of the link i posted says it takes ~2 sec for 700 contacts – pskink Dec 28 '15 at 09:33
  • Yes this does take very less time but when i try to get the event/structured name/organisation and all those things ,on top of this, It takes a lot more time... Is there any way to do that fast too.. – Manvendra Sah Dec 28 '15 at 09:57
  • yes, it will be as fast as light, unless you are playing with things like `Bitmap`s – pskink Dec 28 '15 at 10:01
  • I am not getting any bitmaps or anything.. Am getting the raw-ids associated with the individual contact_id and then getting all data using that raw_id and this for every contact_id.. Is there no way to optimise this... – Manvendra Sah Dec 28 '15 at 10:09
  • did you try my code? – pskink Dec 28 '15 at 10:15
  • I tried ur code.. but i want to get all organisations/events/etc for every raw contact associated with a contact... thats not being done in less time... – Manvendra Sah Dec 28 '15 at 10:24
  • so use `ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE` / `ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE` / etc in `selectionArgs` – pskink Dec 28 '15 at 10:43
  • Referred this link : http://www.higherpass.com/android/tutorials/working-with-android-contacts/ ... Time taken, to get all data, is more than 15s for 100 contacts... – Manvendra Sah Dec 29 '15 at 06:26
  • and my code took 0.012 s (12 ms) for 59 contacts... – pskink Dec 29 '15 at 07:39
  • your code is not getting the events/organisations/structured address/structured name, etc data.. thats why... I need all this data for every raw contact ... Thats what i want...in less time... If u have info regarding exactly that, plzz share it.... – Manvendra Sah Dec 29 '15 at 08:51
  • ok, changed `Uri uri = ContactsContract.Data.CONTENT_URI;`, added `Organization.CONTENT_ITEM_TYPE` and `Event.CONTENT_ITEM_TYPE` and for **2060** contacts it took **434ms**, i could add `structured address/structured name` but i am pretty **sure** it wouldn't change much the results – pskink Dec 29 '15 at 10:01
  • thanks a lot.. will check... – Manvendra Sah Dec 29 '15 at 10:40
  • **UPDATE**, adding `structured name` made it quite longer, but still for **2060** contacts it takes **750/800ms**, so it is **~0.5ms** per single contact (for 100 contacts it would take ~50ms, compared to 15sec in your case == 300 x faster) – pskink Dec 29 '15 at 11:13

1 Answers1

1

This is how I am getting the name & email Address, you can retrieve the other data also

ArrayList<String> emailAddressList = new ArrayList<String>();
ArrayList<String> contactNameList = new ArrayList<String>();

HashSet<String> emlRecsHS = new HashSet<String>();

ContentResolver cr = getContentResolver();
String[] PROJECTION = new String[] {ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.Contacts.DISPLAY_NAME};

String order = "CASE WHEN "
        + ContactsContract.Contacts.DISPLAY_NAME
        + " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
        + ContactsContract.Contacts.DISPLAY_NAME
        + ", "
        + ContactsContract.CommonDataKinds.Email.DATA
        + " COLLATE NOCASE";


String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";

Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);
if (cur.moveToFirst())
{
    do
    {
        // names comes in hand sometimes
        String emlAddress = cur.getString(0);
        String contactName = cur.getString(1);

        // keep unique only
        if (emlRecsHS.add(emlAddress.toLowerCase(Locale.US)))
        {
            emailAddressList.add(emlAddress);
            contactNameList.add(contactName);
        }
    } while (cur.moveToNext());
}

cur.close();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anshuman Borah
  • 538
  • 2
  • 10
  • Thanks for the answer but the problem i am facing is regarding the time consumed in the process... – Manvendra Sah Dec 28 '15 at 09:15
  • @ManvendraSah I tried this in my device, it load all data within a second. – Anshuman Borah Dec 28 '15 at 09:49
  • I need all data including the raw_contact_ids, addresses, structured names, groups, events. organisation,websites,imHandles,etc associated with that raw_contact_id. Can that be done in under 5 sec..? – Manvendra Sah Dec 29 '15 at 06:24