【问题标题】:Android Phone Contacts ONLY with Photo,Phone and NameAndroid 手机联系人仅包含照片、电话和姓名
【发布时间】:2014-04-17 19:05:52
【问题描述】:

我正在使用以下光标来检索仅在手机中的联系人?但是,该代码为我提供了 SIM 卡和电话中的联系人。是否可以仅在电话中获取联系人,包括他们的照片(如果有)、电话号码和显示名称?

  cursor = getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);

  int contactIdIdx = cursor.getColumnIndex(Phone._ID);                  
  int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);                  
  int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);                 
  int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);  
  cursor.moveToFirst();

 do {

    try {

     Contact c = new Contact();

     String idContact = cursor.getString(contactIdIdx);
 String name = cursor.getString(nameIdx);
 c.setName(name);

Log.i(TAG,storage.getDriveName()+": "+name);
    String phoneNumber = cursor.getString(phoneNumberIdx);
    Log.i(TAG,storage.getDriveName()+": "+phoneNumber);
c.setPhoneNumber(phoneNumber);
if(phoneNumber != null)
   c.setPhotoUri(getPhotoUri(Long.valueOf(fetchContactIdFromPhoneNumber(phoneNumber))));

contacts.add(cbd);
   } catch(Exception e) {
  Log.e(TAG,storage.getDriveName()+": "+e.getMessage());
 }
} while (cursor.moveToNext());  
 } catch (Exception e) {
   Log.e(TAG,storage.getDriveName()+": "+e.getMessage());

} 
if (cursor != null) {
cursor.close();
}
 }

【问题讨论】:

    标签: java android android-contacts android-cursoradapter


    【解决方案1】:

    我已经实现了下面的代码对我有用,看看这个:

    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.Contacts.CONTENT_URI);
            // filters contact with phone numbers
            contactPickerIntent
                    .setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
            startActivityForResult(contactPickerIntent, CONTACT_PICKER);
    

    现在在活动结果中:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        AndyUtills.removeSimpleProgressDialog();
    
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case CONTACT_PICKER:
                Cursor cursor = null;
                Bitmap bitmap = null;
    
                Uri result = data.getData();
                // String id = result.getLastPathSegment();
    
                cursor = getContentResolver().query(result, null, null, null,
                        null);
                // String contactId=cursor.get;
                if (cursor == null) {
    
                    AndyUtills.showToast("Selection Fail", this);
                    return;
                }
                if (!cursor.moveToFirst()) {
    
                    cursor.close();
                    AndyUtills.showToast("Selection Fail", this);
                    return;
                }
                String contactName = cursor
                        .getString(cursor
                                .getColumnIndex((ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
                                        .toString().trim()));
                String contactNumber = cursor
                        .getString(cursor
                                .getColumnIndex((ContactsContract.CommonDataKinds.Phone.NUMBER)));
    
                // check for contact whether it is already inserted or not
                if (checkForSameNumber(contactNumber)) {
                    AndyUtills.showToast("Same contact is already inserted",
                            this);
                    return;
                }
                String photo = null;
    
                // get photo uri from contacts
                try {
    
                    photo = cursor
                            .getString(cursor
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Contactables.PHOTO_URI));
                    Log.i("TAG", "Activity Result: Old:" + photo);
    
                } catch (Exception e) {
                    e.printStackTrace();
    
                }
    
                cursor.close();
                break;
    
            }
    
        } else {
            // gracefully handle failure
            Log.w("Auto Respond", "Warning: activity result not ok");
        }
    }
    

    希望你能得到你想要的一切。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-27
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 2011-03-05
      相关资源
      最近更新 更多