title: Android device contacts showing duplicate contact entries
body: <p>I am intending to develop an android app which facilitate to fetch device contacts and show in list.</p>
  
  <p>I am using below code for fetching device contacts it's working fine but showing duplicate entries of contacts.</p>
  
  <pre><code>//FETCH DEVICE CONTACTS
  public void fetchDeviceContacts(){
  
      Constant.progressDialog = ProgressDialog.show(ContactListActivity.this,"", "Please wait...");
  
      Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
      // Querying the table ContactsContract.Contacts to retrieve all the contacts
      final Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null,
              ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
  
      if(contactsCursor.moveToFirst()){
          do{
              long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
  
              Uri dataUri = ContactsContract.Data.CONTENT_URI;
  
              // Querying the table ContactsContract.Data to retrieve individual items like
              // home phone, mobile phone, work email etc corresponding to each contact
              Cursor dataCursor = getContentResolver().query(dataUri, null,
                      ContactsContract.Data.CONTACT_ID + "=" + contactId,
                      null, null);
  
              String nickName="";
              String homePhone="";
              String mobilePhone="";
              String workPhone="";
              byte[] photoByte=null;
              String homeEmail="";
              String workEmail="";
              String companyName="";
              String title="";
  
              if(dataCursor.moveToFirst()){
                  // Getting Display Name
                  contactName= dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
                  do{
  
                      // Getting NickName
                      if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
                          nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
  
                      // Getting Phone numbers
                      if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
                          switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
                          case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
                              homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                              break;
                          case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
                              mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                              break;
                          case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
                              workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                              break;
                          }
                      }
  
                      // Getting EMails
                      if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) {
                          switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
                          case ContactsContract.CommonDataKinds.Email.TYPE_HOME :
                              emailId =homeEmail= dataCursor.getString(dataCursor.getColumnIndex("data1"));
                              break;
                          case ContactsContract.CommonDataKinds.Email.TYPE_WORK :
                              emailId=workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                              break;
                          }
                      }
  
                      // Getting Organization details
                      if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
                          companyName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                          title = dataCursor.getString(dataCursor.getColumnIndex("data4"));
                      }
  
                      // Getting Photo
                      if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){
                          photoByte = dataCursor.getBlob(dataCursor.getColumnIndex("data15"));
  
                          if(photoByte != null) {
                              Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);
  
                              // Getting Caching directory
                              File cacheDirectory = getBaseContext().getCacheDir();
  
                              // Temporary file to store the contact image
                              File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+contactId+".png");
  
                              // The FileOutputStream to the temporary file
                              try {
                                  FileOutputStream fOutStream = new FileOutputStream(tmpFile);
  
                                  // Writing the bitmap to the temporary file as png file
                                  bitmap.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
  
                                  // Flush the FileOutputStream
                                  fOutStream.flush();
  
                                  //Close the FileOutputStream
                                  fOutStream.close();
  
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
                              photoPath = tmpFile.getPath();
  
                          }
                      }
  
  
                      //CREATE CONTACT LIST
                      _contactBean=new ContactBean();
                      if(contactName!=null){
                          _contactBean.setName(contactName);
                      }
                      if(photoPath!=null){
                          _contactBean.setPhotoPath(photoPath);
                      }if(emailId!=null){
                          _contactBean.setEmailId(emailId);
                      }
                      if(contactName!=null){
                          _contactArrayList.add(_contactBean);
                      }
  
                      //////////////////////
  
                  }while(dataCursor.moveToNext());
                  /*String details = "";
  
                          // Concatenating various information to single string
                          if(homePhone != null &amp;&amp; !homePhone.equals("") )
                              details = "HomePhone : " + homePhone + "\n";
                          if(mobilePhone != null &amp;&amp; !mobilePhone.equals("") )
                              details += "MobilePhone : " + mobilePhone + "\n";
                          if(workPhone != null &amp;&amp; !workPhone.equals("") )
                              details += "WorkPhone : " + workPhone + "\n";
                          if(nickName != null &amp;&amp; !nickName.equals("") )
                              details += "NickName : " + nickName + "\n";
                          if(homeEmail != null &amp;&amp; !homeEmail.equals("") )
                              details += "HomeEmail : " + homeEmail + "\n";
                          if(workEmail != null &amp;&amp; !workEmail.equals("") )
                              details += "WorkEmail : " + workEmail + "\n";
                          if(companyName != null &amp;&amp; !companyName.equals("") )
                              details += "CompanyName : " + companyName + "\n";
                          if(title != null &amp;&amp; !title.equals("") )
                              details += "Title : " + title + "\n";*/
  
                  // Adding id, display name, path to photo and other details to cursor
  
              }
  
          }while(contactsCursor.moveToNext());
      }
  
      runOnUiThread(new Runnable() {
          public void run() {
              Constant.progressDialog.cancel();
              contactsCursor.close();
          }
      });
  }
  </code></pre>
  
  <p>help will be appreciated. Thanks</p>