Sunday, 8 June 2014

READ CONTACT INFORMATION ( NAME, NICKNAME, NUMBER, EMAIL, ADDRESS, BIRTHDAY, ANNIVERSARY, NOTE, ORGANISATION, ACCOUNTS) IN ANDROID


In this tutorial we will read the contact information present in android device like name, nickname, number,
pic, address, birthday, anniversary, note, organisation, accounts.
Download Source Code Download

For reading contacts we require permission READ_CONTACTS and for reading accounts GET_ACCOUNTS so add these two permissions in AndroidManifest.xml


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


AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorialsface.contact"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.tutorialsface.contact.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

Create a model class ContactData.java.

ContactData.java

import android.graphics.Bitmap;

public class ContactData {
      private int id;
      private String name;
      String nickName;
      private String phoneNumber;
      private int favourite;
      private int type;
      private String label;
      private int account_id;
      private int system_contact_id;
      private String account_name;
      private String account_type;
      private String company;
      private String title;
      private String note;
      private String date;
      private String email;
      private String street;
      private String poBox;
      private String neighborhood;
      private String city;
      private String region;
      private String postCode;
      private String country;
      private Bitmap image;

      public int getId() {
            return id;
      }
      public void setId(int id) {
            this.id = id;
      }
      public String getName() {
            return name;
      }
      public void setName(String name) {
            this.name = name;
      }
      public int getFavourite() {
            return favourite;
      }
      public void setFavourite(int favourite) {
            this.favourite = favourite;
      }
      public int getType() {
            return type;
      }
      public void setType(int type) {
            this.type = type;
      }
      public String getLabel() {
            return label;
      }
      public void setLabel(String label) {
            this.label = label;
      }
      public int getAccount_id() {
            return account_id;
      }
      public void setAccount_id(int account_id) {
            this.account_id = account_id;
      }
      public int getSystem_contact_id() {
            return system_contact_id;
      }
      public void setSystem_contact_id(int system_contact_id) {
            this.system_contact_id = system_contact_id;
      }
      public String getAccount_name() {
            return account_name;
      }
      public void setAccount_name(String account_name) {
            this.account_name = account_name;
      }
      public String getAccount_type() {
            return account_type;
      }
      public void setAccount_type(String account_type) {
            this.account_type = account_type;
      }
      public String getCompany() {
            return company;
      }
      public void setCompany(String company) {
            this.company = company;
      }
      public String getTitle() {
            return title;
      }
      public void setTitle(String title) {
            this.title = title;
      }
      public String getNote() {
            return note;
      }
      public void setNote(String note) {
            this.note = note;
      }
      public String getDate() {
            return date;
      }
      public void setDate(String date) {
            this.date = date;
      }
      public String getEmail() {
            return email;
      }
      public void setEmail(String email) {
            this.email = email;
      }
      public String getNickName() {
            return nickName;
      }
      public void setNickName(String nickName) {
            this.nickName = nickName;
      }
      public String getStreet() {
            return street;
      }
      public void setStreet(String street) {
            this.street = street;
      }
      public String getPoBox() {
            return poBox;
      }
      public void setPoBox(String poBox) {
            this.poBox = poBox;
      }
      public String getNeighborhood() {
            return neighborhood;
      }
      public void setNeighborhood(String neighborhood) {
            this.neighborhood = neighborhood;
      }
      public String getCity() {
            return city;
      }
      public void setCity(String city) {
            this.city = city;
      }
      public String getRegion() {
            return region;
      }
      public void setRegion(String region) {
            this.region = region;
      }
      public String getPostCode() {
            return postCode;
      }
      public void setPostCode(String postCode) {
            this.postCode = postCode;
      }
      public String getCountry() {
            return country;
      }
      public void setCountry(String country) {
            this.country = country;
      }
      public String getPhoneNumber() {
            return phoneNumber;
      }
      public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
      }
      public Bitmap getImage() {
            return image;
      }
      public void setImage(Bitmap image) {
            this.image = image;
      }
}

And lastly the PhoneContact.java in which all function will be written for fetching data and store in ArrayList.

PhoneContact.java

import java.util.ArrayList;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;

public class PhoneContact {
      String LOG_CLASS = "PhoneContact";
     
      Context context;
      ContentResolver cr;
      ArrayList<ContactData> accountMapList = null;
      ArrayList<ContactData> accountList = null;
      ArrayList<ContactData> organisationList = null;
      ArrayList<ContactData> noteList = null;
      ArrayList<ContactData> eventList = null;
      ArrayList<ContactData> addressList = null;
      ArrayList<ContactData> emailList = null;
      ArrayList<ContactData> phoneList = null;
      ArrayList<ContactData> contactList = null;
      ArrayList<ContactData> favouriteList = null;
      ArrayList<ContactData> imageList = null;
     
      public PhoneContact(Context context){
            this.context = context;
            cr = context.getContentResolver();
            accountMapList = new ArrayList<ContactData>();
            accountList = new ArrayList<ContactData>();
            organisationList = new ArrayList<ContactData>();
            noteList = new ArrayList<ContactData>();
            eventList = new ArrayList<ContactData>();
            addressList = new ArrayList<ContactData>();
            emailList = new ArrayList<ContactData>();
            phoneList = new ArrayList<ContactData>();
            contactList = new ArrayList<ContactData>();
            favouriteList = new ArrayList<ContactData>();
            imageList = new ArrayList<ContactData>();
            contactDetails();
      }
     
      public void contactDetails(){
            Uri URI_NICK_NAME = ContactsContract.Data.CONTENT_URI;
           
            readAccountNames();
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

            if(cursor.getCount() > 0){
                  Log.v(LOG_CLASS, LOG_CLASS +"contactNumbers->" + cursor.getCount());
                  cursor.moveToFirst();
                  while(!cursor.isAfterLast()){
                        //ContactId of Contact
                        int systemContactId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        Log.v(LOG_CLASS, LOG_CLASS +"Contact Id:- " + systemContactId);
                        //Name of Contact
                        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        Log.v(LOG_CLASS, LOG_CLASS +"Name:- " + name);
                        String nickName = "";
                        //NickName of Contact
                        String SELECTION_NICK_NAME = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                        String[] SELECTION_ARRAY_NICK_NAME = new String[] {
                                                            String.valueOf(systemContactId),
                                                 ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE };
                       
                        Cursor currNickName = cr.query(URI_NICK_NAME, null,
                                                 SELECTION_NICK_NAME, SELECTION_ARRAY_NICK_NAME,
                                                 null);
                        if (currNickName.getCount() > 0) {
                              currNickName.moveToFirst();
                              int indexNickName = currNickName.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME);
                              nickName = currNickName.getString(indexNickName);
                              Log.v(LOG_CLASS, LOG_CLASS +"Nick Name:- " + nickName);
                        }
                        currNickName.close();
                        ContactData dataContact = new ContactData();
                        dataContact.setSystem_contact_id(systemContactId);
                        dataContact.setName(name);
                        dataContact.setNickName(nickName);
                        contactList.add(dataContact);
                        //Number of Contact(cc,actualNumber, label, type, fav)
                        readContactNumbers(systemContactId);
                        //Email of Contact(email, label, type)
                readEmailAddress(systemContactId);
                //Address of Contact(street, poBox, neighborhood, city, region, postCOde, country,label, type)
                readAddress(systemContactId);
                //Event of Contact(date, label, type)
                readEvents(systemContactId);
                        //Note of contact(note)
                readNote(systemContactId);
                //Organisation of contact(company, title, type, label)
                        readOrganisation(systemContactId);
                        //Account of contact(accountId, contactId)
                        readAccountMapping(systemContactId);
                        //Image of contact
                        getImageOfContact(systemContactId);
                        cursor.moveToNext();
                  }
                  cursor.close();
            }
            //read favourite Contacts
            readFavouriteContacts();
      }
     
      private void readContactNumbers(int systemContactId) {
            Uri URI_PHONE = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] PROJECTION_PHONE = new String[] {
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.LABEL,
                ContactsContract.CommonDataKinds.Phone.TYPE};
           String SELECTION_PHONE = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";
           String[] SELECTION_ARRAY_PHONE = new String[] { String.valueOf(systemContactId) };
           
            Cursor curPhone = cr.query(URI_PHONE, PROJECTION_PHONE, SELECTION_PHONE, SELECTION_ARRAY_PHONE, null);
            if(curPhone.getCount() > 0 ){
                  curPhone.moveToFirst();
                  int indexNumber = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
              int labelIndex = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
                  int indexPhoneType = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
                  while(!curPhone.isAfterLast()){
                        ContactData dataPhone = new ContactData();
                  String contactNumber = curPhone.getString(indexNumber);
                  String labelName = "";
                  int labelType = curPhone.getInt(indexPhoneType);
                  if(labelType == ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM){
                        labelName = curPhone.getString(labelIndex);
                  }
                  dataPhone.setSystem_contact_id(systemContactId);
                  dataPhone.setPhoneNumber(contactNumber);
                  dataPhone.setType(labelType);
                  dataPhone.setLabel(labelName);
                  phoneList.add(dataPhone);
                  Log.v(LOG_CLASS, LOG_CLASS +"Number:- " + contactNumber + "[" + labelType + "] " + labelName);
                        curPhone.moveToNext();
                  }
            }
            curPhone.close();
      }
     
      private void readEmailAddress(int systemContactId) {
            Uri URI_EMAIL = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
            String[] PROJECTION_EMAIL = new String[]{
                        ContactsContract.CommonDataKinds.Email.DATA,
                        ContactsContract.CommonDataKinds.Email.TYPE,
                        ContactsContract.CommonDataKinds.Email.LABEL};
            String SELECTION_EMAIL = ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = ?";
        String[] SELECTION_ARRAY_EMAIL = new String[] { String.valueOf(systemContactId) };
        Cursor curEmail = cr.query(URI_EMAIL, PROJECTION_EMAIL, SELECTION_EMAIL, SELECTION_ARRAY_EMAIL, null);
        if(curEmail.getCount() > 0){
            curEmail.moveToFirst();
            int indexEmail = curEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
            int indexEmailType = curEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE);
            int indexEmailLabel = curEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.LABEL);
            while(!curEmail.isAfterLast()){
                  ContactData dataEmail= new ContactData();
                  String labelName = "";
                  String emailStr = curEmail.getString(indexEmail);
                int emailType = curEmail.getInt(indexEmailType);
                if(emailType == ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM){
                        labelName = curEmail.getString(indexEmailLabel);
                  }
                dataEmail.setEmail(emailStr);
                dataEmail.setType(emailType);
                dataEmail.setLabel(labelName);
                emailList.add(dataEmail);
                Log.v(LOG_CLASS, LOG_CLASS +"Email:- " + emailStr + "[" + emailType + "] " + labelName);
                  curEmail.moveToNext();
            }
        }
        curEmail.close();
      }
     
      private void readAddress(int systemContactId) {
            Uri URI_ADDRESS = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
            String SELECTION_ADDRESS = ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = ? AND " +
                  ContactsContract.CommonDataKinds.StructuredPostal.MIMETYPE + " = ?";
        String[] SELECTION_ARRAY_ADDRESS = new String[]{
                        String.valueOf(systemContactId),
                        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
        Cursor currAddr = cr.query(URI_ADDRESS, null,SELECTION_ADDRESS, SELECTION_ARRAY_ADDRESS, null);
        if (currAddr.getCount() > 0) {
            currAddr.moveToFirst();
            int indexAddType = currAddr.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE);
            int indexAddLabel = currAddr.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.LABEL);
            int indexStreet = currAddr.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET);
            int indexPOBox = currAddr.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX);
            int indexNeighbor = currAddr.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD);
            int indexCity = currAddr.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY);
            int indexRegion = currAddr.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION);
            int indexPostCode = currAddr.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE);
            int indexCountry = currAddr.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY);
            while (!currAddr.isAfterLast()) {
                  ContactData dataAddress = new ContactData();
                  String street = currAddr.getString(indexStreet);
                  String poBox = currAddr.getString(indexPOBox);
                  String neighbor = currAddr.getString(indexNeighbor);
                  String city = currAddr.getString(indexCity);
                  String region = currAddr.getString(indexRegion);
                  String postCode = currAddr.getString(indexPostCode);
                  String country = currAddr.getString(indexCountry);
                  int type = currAddr.getInt(indexAddType);
                  String labelName = "";
                  if(type == ContactsContract.CommonDataKinds.StructuredPostal.TYPE_CUSTOM){
                        labelName = currAddr.getString(indexAddLabel);
                  }
                  dataAddress.setSystem_contact_id(systemContactId);
                  dataAddress.setStreet(street);
                  dataAddress.setPoBox(poBox);
                  dataAddress.setNeighborhood(neighbor);
                  dataAddress.setCity(city);
                  dataAddress.setRegion(region);
                  dataAddress.setPostCode(postCode);
                  dataAddress.setCountry(country);
                  dataAddress.setType(type);
                  dataAddress.setLabel(labelName);
                  addressList.add(dataAddress);
                  Log.v(LOG_CLASS, LOG_CLASS +"Address:- " + street + "," + poBox + "," + neighbor + "," + city + "," + region + "," + postCode + "," + country +
                                          "[" + type + "] " + labelName);
                  currAddr.moveToNext();
            }
        }
        currAddr.close();
      }
     
      private void readEvents(int systemContactId) {
            Uri URI_EVENT = ContactsContract.Data.CONTENT_URI;
            String SELECTION_EVENT = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ? ";
            String[] SELECTION_ARRAY_EVENT = new String[] {
                                                      String.valueOf(systemContactId),
                                          ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
                 
            Cursor currEvent = cr.query(URI_EVENT, null, SELECTION_EVENT,SELECTION_ARRAY_EVENT, null);
            if (currEvent.getCount() > 0) {
                  currEvent.moveToNext();
                  int indexEvent = currEvent.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
                  int indexType = currEvent.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE);
                  int indexLabel = currEvent.getColumnIndex(ContactsContract.CommonDataKinds.Event.LABEL);
                  while(!currEvent.isAfterLast()){
                        ContactData dataEvent = new ContactData();
                        String date = currEvent.getString(indexEvent);
                        int type = currEvent.getInt(indexType);
                        String label = "";
                        if(type == ContactsContract.CommonDataKinds.Event.TYPE_CUSTOM){
                              label = currEvent.getString(indexLabel);
                        }
                        dataEvent.setSystem_contact_id(systemContactId);
                        dataEvent.setDate(date);
                        dataEvent.setType(type);
                        dataEvent.setLabel(label);
                        eventList.add(dataEvent);
                        Log.v(LOG_CLASS, LOG_CLASS +"Event:- " + date + "[" + type + "] " + label);
                        currEvent.moveToNext();
                  }
            }
            currEvent.close();
      }
     
     
      private void readNote(int systemContactId) {
            Uri URI_NOTE = ContactsContract.Data.CONTENT_URI;
             String SELECTION_NOTE = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
         String[] SELECTION_ARRAY_NOTE = new String[] {
                                          String.valueOf(systemContactId),
                                 ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE };
         Cursor currNote = cr.query(URI_NOTE, null, SELECTION_NOTE,SELECTION_ARRAY_NOTE, null);
         if(currNote.getCount() > 0){
            ContactData dataNote = new ContactData();
            currNote.moveToFirst();
            int indexNote = currNote.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE);
            String note = currNote.getString(indexNote);
            dataNote.setSystem_contact_id(systemContactId);
            dataNote.setNote(note);
            Log.v(LOG_CLASS, LOG_CLASS +"Note:- " + note);
            noteList.add(dataNote);
         }
         currNote.close();
      }
     
      private void readOrganisation(int systemContactId) {
            Uri URI_ORGNIZATION = ContactsContract.Data.CONTENT_URI;
            String SELECTION_ORGNIZATION = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
            String[] SELECTION_ARRAY_ORGNIZATION = new String[] {
                                                      String.valueOf(systemContactId),
                                          ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE };
                 
            Cursor currOrg = cr.query(URI_ORGNIZATION, null,SELECTION_ORGNIZATION, SELECTION_ARRAY_ORGNIZATION, null);
            if(currOrg.getCount() > 0){
                  int indexOrgType = currOrg.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TYPE);
                  int indexOrgLabel = currOrg.getColumnIndex(ContactsContract.CommonDataKinds.Organization.LABEL);
                  int indexOrgName = currOrg.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY);
                  int indexOrgTitle = currOrg.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE);
                  currOrg.moveToFirst();
                  while(!currOrg.isAfterLast()){
                        ContactData dataOrg = new ContactData();
                        String company = currOrg.getString(indexOrgName);
                        String title = currOrg.getString(indexOrgTitle);
                        int type = currOrg.getInt(indexOrgType);
                        String label = "";
                        if(type == ContactsContract.CommonDataKinds.Organization.TYPE_CUSTOM){
                              label = currOrg.getString(indexOrgLabel);
                        }
                        dataOrg.setSystem_contact_id(systemContactId);
                        dataOrg.setCompany(company);
                        dataOrg.setTitle(title);
                        dataOrg.setType(type);
                        dataOrg.setLabel(label);
                        organisationList.add(dataOrg);
                        Log.v(LOG_CLASS, LOG_CLASS +"Organisation:- " + company + ", " + title + "[" + type + "] " + label);
                        currOrg.moveToNext();
                  }
            }
            currOrg.close();
      }
     
      private void readAccountMapping(int systemContactId) {
            Uri URI_ACCOUNTS = ContactsContract.RawContacts.CONTENT_URI;
            String[] PROJECTION_ACCOUNT = new String[] {
                        ContactsContract.RawContacts.ACCOUNT_NAME,
                        ContactsContract.RawContacts.ACCOUNT_TYPE};
            String SELECTION_ACCOUNT = ContactsContract.RawContacts.CONTACT_ID + " = ?";
            String[] SELECTION_ARRAY_ACCOUNT = new String[] { String.valueOf(systemContactId) };
            Cursor cursorAccount = cr.query(URI_ACCOUNTS,PROJECTION_ACCOUNT, SELECTION_ACCOUNT, SELECTION_ARRAY_ACCOUNT, null);
            if (cursorAccount.getCount() > 0) {
                  cursorAccount.moveToFirst();
                  while(!cursorAccount.isAfterLast()){
                        ContactData data = new ContactData();
                        String name = cursorAccount.getString(cursorAccount.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME));
                        String type = cursorAccount.getString(cursorAccount.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
                        data.setAccount_name(name);
                        data.setAccount_type(type);
                        data.setSystem_contact_id(systemContactId);
                        accountMapList.add(data);
                        cursorAccount.moveToNext();
                  }
            }
            cursorAccount.close();
      }

      private void readAccountNames() {
            AccountManager am = AccountManager.get(context);
            Account[] accounts = am.getAccounts();
            for(Account acc : accounts){
                  ContactData data = new ContactData();
                  data.setAccount_name(acc.name);
                  data.setAccount_type(acc.type);
                  accountList.add(data);
            }
      }
     
      private void readFavouriteContacts() {
            Uri URI_STARRED = ContactsContract.Contacts.CONTENT_URI;
            String[] PROJECTION_STARRED = new String[]{ContactsContract.Contacts._ID};
            String SELECTION_STARRED = ContactsContract.Contacts.STARRED + " = ?";
            String[] SELECTION_ARRAY_STARRED = new String[]{"1"};
            Cursor cursorStarred = cr.query(URI_STARRED, PROJECTION_STARRED, SELECTION_STARRED, SELECTION_ARRAY_STARRED, null);
            if(cursorStarred.getCount() > 0){
                  cursorStarred.moveToFirst();
                  int indexContactId = cursorStarred.getColumnIndex(ContactsContract.Contacts._ID);
                  while(!cursorStarred.isAfterLast()){
                        ContactData data = new ContactData();
                        int id = cursorStarred.getInt(indexContactId);
                        data.setSystem_contact_id(id);
                        favouriteList.add(data);
                        Log.v(LOG_CLASS, LOG_CLASS +"Favourite Contact ID:- " + id);
                        cursorStarred.moveToNext();
                  }
            }
            cursorStarred.close();
      }
     
      private void getImageOfContact(int id){
             Uri URI_PHOTO = ContactsContract.Data.CONTENT_URI;
         String SELECTION_PHOTO = ContactsContract.Data.CONTACT_ID
                                     + " = ? AND " + ContactsContract.Data.MIMETYPE
                                     + " = ?";
         String[] SELECTION_ARRAY_PHOTO = new String[] {
                                     String.valueOf(id),
                                     ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE };

         Cursor currPhoto = cr.query(URI_PHOTO, null,SELECTION_PHOTO, SELECTION_ARRAY_PHOTO, null);
         int indexPhoto = currPhoto.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO);

         if(currPhoto.getCount() > 0){
             currPhoto.moveToFirst();
             byte[] photoByte = currPhoto.getBlob(indexPhoto);
             if (photoByte != null) {
                 ContactData data = new ContactData();
                   Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);
                 data.setImage(bitmap);
                 data.setSystem_contact_id(id);
                 imageList.add(data);
             }
         }
         currPhoto.close();
      }
}

Now you can call it like this way  
PhoneContact pc = new PhoneContact(getApplicationContext());
and see the logcat it will show all the details.

Accessing, processing, and writing out the Contacts entries can take 5-10 seconds for a typical phone with several hundred entries. If this were run on the main UI thread it would lock up the UI for that period. Thus, call it in background thread. 

No comments:

Post a Comment