title: displaying contacts in gmail and phone's contact using Cursor android
body: <p>I tried to display contacts from phone in my app, But its showing both contacts from gmail and phone. But my emulator only shows phone contact not gmail contact.</p>
  
  <p>How to avoid gmail contacts not to be selected. I need only contacts from phone.</p>
  
  <p>Contacts from phone are:</p>
  
  <p><img src="https://i.stack.imgur.com/hW9mI.png" alt="contacts from phone"></p>
  
  <p>contacts showing in my app is,</p>
  
  <p><img src="https://i.stack.imgur.com/CzRDp.png" alt="contacts showing in my app"></p>
  
  <p>My code is,</p>
  
  <pre><code>private class ListViewContactsLoader extends AsyncTask&lt;Void, Void, Cursor&gt;{
  
      @Override
      protected Cursor doInBackground(Void... params) {
          Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
  
          // Querying the table ContactsContract.Contacts to retrieve all the contacts
          Cursor contactsCursor = getActivity().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 = getActivity().getContentResolver().query(dataUri, null,
                                          ContactsContract.Data.CONTACT_ID + "=" + contactId,
                                          null, null);
  
                  String displayName="";
                  photoPath="" + R.drawable.noimage;
                  byte[] photoByte=null;
  
                  if(dataCursor.moveToFirst()){
                      // Getting Display Name
                      displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
                      do{
                          // Getting Phone numbers
                          if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
                              number = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                          }
                          // 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 = getActivity().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();
                              }
                          }
                      }while(dataCursor.moveToNext());
  
                      // Adding id, display name, path to photo and other details to cursor
                      mMatrixCursor.addRow(new Object[]{Long.toString(contactId), displayName, number, photoPath});
  
                  }
              }while(contactsCursor.moveToNext());
          }
          return mMatrixCursor;
      }
  </code></pre>
  
  <p>How to avoid contacts from gmail. Is this possible.</p>