2015-09-20 143 views
0

我想從電話聯繫人列表中顯示姓名,電話號碼,圖像和電子郵件。不幸的是,圖片和電子郵件不會從聯繫人中檢索。聯繫人姓名和電話號碼正確顯示。從電話聯繫人獲取電子郵件和照片不起作用

在我的代碼一看,

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    selectUsers = new ArrayList<SelectUser>(); 
    resolver = getContentResolver(); 

    contacts_list = (ListView) findViewById(R.id.contacts_list); 

    phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 
    LoadContact loadContact = new LoadContact(); 
    loadContact.execute(); 
} 

class LoadContact extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

    } 

    @Override 
    protected Void doInBackground(Void... voids) { 
     // Get Contact list from Phone 

     if (phones != null) { 
      Log.e("count", "" + phones.getCount()); 
      if (phones.getCount() == 0) { 

      } 

      while (phones.moveToNext()) { 
       String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); 
       String selected_name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       String EmailAddr = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
       String image_thumb = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI)); 

       try { 
        if (image_thumb != null) { 
         bit_thumb = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb)); 
        } else { 
         Log.e("No Image Thumb", "--------------"); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       SelectUser selectUser = new SelectUser(); 
       selectUser.setThumb(bit_thumb); 
       selectUser.setName(selected_name); 
       selectUser.setPhone(phoneNumber); 
       selectUser.setEmail(EmailAddr); 
       selectUser.setCheckedBox(false); 
       selectUsers.add(selectUser); 
      } 
     } else { 
      Log.e("Cursor close 1", "----------------"); 
     } 
     //phones.close(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     adapter = new SelectUserAdapter(selectUsers, MainActivity.this); 
     contacts_list.setAdapter(adapter); 

     // Select item on listclick 
     contacts_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

       Log.e("search", "here---------------- listener"); 

       data = selectUsers.get(i); 

       String phoneNumber = data.getPhone(); 
       String email = data.getEmail(); 
       String name = data.getName(); 

       Intent i1 = new Intent(MainActivity.this, SingleContact.class); 
       i1.putExtra("name", name); 
       i1.putExtra("phone_number", phoneNumber); 
       i1.putExtra("email", email); 
       startActivity(i1); 
      } 
     }); 

     contacts_list.setFastScrollEnabled(true); 
    } 
} 

哪些錯誤在我的代碼。任何人都可以知道這個問題。請在我的代碼中編輯。

回答

1

使用以下方法從聯繫人獲取電子郵件和照片。

public ArrayList<String> getEmailAndPhoto(){ 
     ArrayList<String> names = new ArrayList<String>(); 
     ContentResolver cur = getContentResolver(); 
     Cursor cursor = cur.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
     if (cursor.getCount() > 0) { 
      while (cursor.moveToNext()) { 
       String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
       Cursor cur1 = cur.query( 
         ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, 
         ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
           new String[]{id}, null); 
       while (cur1.moveToNext()) { 
        String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
        Log.e("Name :", name); 
        String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
        String photo = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Photo.DATA1)); 
        Log.e("Email", email); 
        if(email!=null){ 
         names.add(email); 
        } 
        // if(photo!=null){ //for getting photo. 
        // names.add(photo); 
        // } 
       } 
       cur1.close(); 
      } 
     } 
     return names; 
    } 

就是這樣。

+0

這是非常緩慢的**訪問數據的方法,因爲您必須運行'N' + 1'query'調用,其中'N'是聯繫人的數量 – pskink

0

經過大量的搜索我得到了答案。任何人有同樣的問題可以參考這個,

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    selectUsers = new ArrayList<SelectUser>(); 
    resolver = getContentResolver(); 

    contacts_list = (ListView) findViewById(R.id.contacts_list); 

    phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 
    LoadContact loadContact = new LoadContact(); 
    loadContact.execute(); 
} 

class LoadContact extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

    } 

    @Override 
    protected Void doInBackground(Void... voids) { 
     // Get Contact list from Phone 

     if (phones != null) { 
      Log.e("count", "" + phones.getCount()); 
      if (phones.getCount() == 0) { 

      } 

      while (phones.moveToNext()) { 
       String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); 
       String selected_name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       String EmailAddr = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
       String image_thumb = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI)); 

       try { 
        if (image_thumb != null) { 
         bit_thumb = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb)); 
        } else { 
         Log.e("No Image Thumb", "--------------"); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       SelectUser selectUser = new SelectUser(); 
       selectUser.setThumb(bit_thumb); 
       selectUser.setName(selected_name); 
       selectUser.setPhone(phoneNumber); 
       Cursor emailCursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null); 

       while (emailCursor.moveToNext()) { 
        emails = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
        System.out.println("Email : " + emails); 
        selectUser.setEmail(emails); 
       } 
       emailCursor.close(); 

       selectUser.setCheckedBox(false); 
       selectUsers.add(selectUser); 
      } 
     } else { 
      Log.e("Cursor close 1", "----------------"); 
     } 
     //phones.close(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     adapter = new SelectUserAdapter(selectUsers, MainActivity.this); 
     contacts_list.setAdapter(adapter); 

     // Select item on listclick 
     contacts_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

       Log.e("search", "here---------------- listener"); 

       data = selectUsers.get(i); 

       String phoneNumber = data.getPhone(); 
       String email = data.getEmail(); 
       String name = data.getName(); 

       Intent i1 = new Intent(MainActivity.this, SingleContact.class); 
       i1.putExtra("name", name); 
       i1.putExtra("phone_number", phoneNumber); 
       i1.putExtra("email", email); 
       startActivity(i1); 
      } 
     }); 

     contacts_list.setFastScrollEnabled(true); 
    } 
} 
+0

這是極其緩慢**訪問數據的方法,因爲您必須運行'N' + 1'query'調用,其中'N'是聯繫人數量,也不會找到沒有電話的用戶號碼 – pskink

+0

請告訴我爲什麼你要使用這種加載聯繫人數據的緩慢方法,爲什麼? – pskink

+0

@pskink:其實我只知道這種方法。 –

相關問題