title: Getting null value while retrieving the CONTACT NAME from CONTACT EMAIL
body: <p>I want to retrieve the contact name from the contact email, so what I have done is as follows from <a href="https://stackoverflow.com/a/18064869/5738881">https://stackoverflow.com/a/18064869/5738881</a>.</p>
  
  <pre><code> public static String readContacts(Context context, String email) {
      ContentResolver cr = context.getContentResolver();
      Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
      Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
      if (cursor == null) {
          return null;
      }
      String contactName = null;
      if (cursor.moveToFirst()) {
          contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
      }
  
      if (!cursor.isClosed()) {
          cursor.close();
      }
  
      Log.e("....contact name....", email + "\n" + contactName);
  
      return contactName;
  
  }
  </code></pre>
  
  <p>Then in onCreate(), I have coded as,</p>
  
  <pre><code> sName = readContacts(getApplicationContext(), sEmail);
  
      etName.setText(sName);
  </code></pre>
  
  <p>But I am getting the null value. Therefore what could be the soution to fetch contact name, depending upon just the contact email address?</p>
  
  <blockquote>
    <p>EDIT-1:</p>
  </blockquote>
  
  <p>I have already mentioned the permission in manifest as,</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.READ_CONTACTS"/&gt;
  </code></pre>
  
  <blockquote>
    <p>EDIT-2:</p>
  </blockquote>
  
  <p>As per <strong><em>ROHIT SHARMA</em></strong>'s answer, I changed my code as below.</p>
  
  <pre><code> public static String readContacts(Context context, String email) {
      ContentResolver cr = context.getContentResolver();
      Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
      Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
      if (cursor == null) {
          return null;
      }
      String contactName = null;
  
      if (cursor.getCount() &gt; 0) {
          cursor.moveToFirst();
  
      } else {
          return null;
      }
  
      if (cursor.moveToFirst()) {
          contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
      }
  
      if (!cursor.isClosed()) {
          cursor.close();
      }
  
      Log.e("....contact name....", email + "\n" + contactName);
  
      return contactName;
  
  }
  </code></pre>
  
  <p>But it didn't help me as well.</p>
  
  <blockquote>
    <p>EDIT-3:</p>
  </blockquote>
  
  <pre><code>public static String readContacts(Context context, String email) {
  ContentResolver cr = context.getContentResolver();
  Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
  Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
  String contactName = null;
  if(cursor!=null &amp;&amp; cursor.getCount()&gt;0 )
  { 
   cursor.moveToFirst();
   contactName =  cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
  }else{ 
     return null; 
  } 
  if (!cursor.isClosed()) {
      cursor.close();
  } 
  
  Log.e("....contact name....", email + "\n" + contactName);
  
  return contactName;
  
  }
  </code></pre>
  
  <p>It didn't help me as well.</p>
  
  <blockquote>
    <p>EDIT-4:</p>
  </blockquote>
  
  <p>I tried</p>
  
  <pre><code> public String readContacts(Context context, String email) {
  
      String name = null;
  
      // define the columns I want the query to return
      String[] projection = new String[]{
              ContactsContract.PhoneLookup.DISPLAY_NAME,
              ContactsContract.PhoneLookup._ID};
  
      // encode the email and build the filter URI
      Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
  
      // query time
      Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
  
      if (cursor != null) {
          if (cursor.moveToFirst()) {
              name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
              Log.e("....email.....", "Started uploadcontactphoto: Contact Found @ " + email);
              Log.e("....name....", "Started uploadcontactphoto: Contact name  = " + name);
          } else {
              Log.e("....email exception....", "Contact Not Found @ " + email);
          }
          cursor.close();
      }
      return name;
  
  }
  </code></pre>
  
  <p>from <a href="https://stackoverflow.com/a/15007980/5738881">https://stackoverflow.com/a/15007980/5738881</a> but didn't help as well.</p>
  
  <p>Anybody got other way out?</p>
  
  <blockquote>
    <p>EDIT-5:</p>
  </blockquote>
  
  <pre><code>public String readContacts() {
  
      ContentResolver cr = getContentResolver();
  
      @SuppressLint("Recycle")
  
      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
              null, null, null, null);
  
      String id, name = null, email = null;
  
      if (cur != null &amp;&amp; cur.getCount() &gt; 0) {
          while (cur.moveToNext()) {
              id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
              name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
              if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) &gt; 0) {
                  System.out.println("name : " + name + ", ID : " + id);
  
                  // get the phone number
                  Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                          ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                          new String[]{id}, null);
                  if (pCur != null) {
                      while (pCur.moveToNext()) {
                          String phone = pCur.getString(
                                  pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                          System.out.println("phone" + phone);
                      }
                  }
                  if (pCur != null) {
                      pCur.close();
                  }
  
  
                  // get email and type
  
                  Cursor emailCur = cr.query(
                          ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                          null,
                          ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                          new String[]{id}, null);
                  if (emailCur != null) {
                      while (emailCur.moveToNext()) {
                          // This would allow you get several email addresses
                          // if the email addresses were stored in an array
                          email = emailCur.getString(
                                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                          String emailType = emailCur.getString(
                                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
  
  
                          System.out.println("Email " + email + " Email Type : " + emailType);
                      }
  
                      if (email == sEmail) {
                          sName = name;
                      }
  
                  }
                  if (emailCur != null) {
                      emailCur.close();
                  }
              }
          }
      }
      return name;
  }
  </code></pre>
  
  <p>I tried above code instance from <a href="http://www.coderzheaven.com/2011/06/13/get-all-details-from-contacts-in-android/" rel="nofollow noreferrer">http://www.coderzheaven.com/2011/06/13/get-all-details-from-contacts-in-android/</a> and I am getting name, but it is different name than the email id owner.</p>
  
  <p>So, tell me where I am going wrong..</p>
  
  <p>And from above code snippet, I have also tried</p>
  
  <pre><code>if (email == sEmail) {
                              sName = name;
                          }
                          System.out.println("Email " + email + " Email Type : " + emailType);
                      }
  
  
  
                  }
                  if (emailCur != null) {
                      emailCur.close();
                  }
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>                        System.out.println("Email " + email + " Email Type : " + emailType);
                      }
  
                      if (email == sEmail) {
                          sName = name;
                      }
  
                  }
                  if (emailCur != null) {
                      emailCur.close();
                  }
  </code></pre>
  
  <p>but didn't help as well.</p>