从Android中的联系人列表中获取联系人
Posted
技术标签:
【中文标题】从Android中的联系人列表中获取联系人【英文标题】:Fetching contact from Contact list in Android 【发布时间】:2016-10-08 12:58:36 【问题描述】:public class PhoneActivity extends Activity
private static final String TAG = PhoneActivity.class.getSimpleName();
private static final int REQUEST_CODE_PICK_CONTACTS = 1;
private Uri uriContact;
private String contactID;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
public void onClickSelectContact(View btnSelectContact)
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI),REQUEST_CODE_PICK_CONTACTS);
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK)
Log.d(TAG,"Response:" + data.toString());
uriContact = data.getData();
retrieveContactName();
retrieveContactNumber();
retrieveContactPhoto();
private void retrieveContactPhoto()
Bitmap photo = null;
try
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,new Long(contactID)));
if(inputStream != null)
photo = BitmapFactory.decodeStream(inputStream);
ImageView imageView = (ImageView) findViewById(R.id.img_contact);
imageView.setImageBitmap(photo);
assert inputStream != null;
inputStream.close();
catch (IOException e)
e.printStackTrace();
private void retrieveContactNumber()
String contactNumber = null;
Cursor cursorID = getContentResolver().query(uriContact,
new String[]ContactsContract.Contacts._ID,
null,null,null);
if(cursorID.moveToFirst())
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
cursorID.close();
Log.d(TAG,"Contact ID:" + contactID);
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + "=" +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]contactID,null);
if (cursorPhone.moveToFirst())
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
cursorPhone.close();
Log.d(TAG, "Contact Phone Number: " + contactNumber);
private void retrieveContactName()
String contactName = null;
Cursor cursor = getContentResolver().query(uriContact, null, null, null,null);
if(cursor.moveToFirst())
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
cursor.close();
Log.d(TAG, "Contact Name : " + contactName);
当我在联系人中选择联系人时应用程序崩溃。问题在于将结果返回给调用的活动。我已发布 logcat
06-08 13:16:06.488 1520 1520 D PhoneActivity: Contact Name : Jeevankumar M
06-08 13:16:06.515 1520 1520 D PhoneActivity: Contact ID:1
06-08 13:16:06.554 1520 1520 D PhoneActivity: Contact Phone Number: (974) 303-3493
06-08 13:16:06.575 1272 1272 I Choreographer: Skipped 54 frames! The application may be doing too much work on its main thread.
06-08 13:16:06.721 1520 1520 D androidRuntime: Shutting down VM
06-08 13:16:06.731 1520 1520 E AndroidRuntime: FATAL EXCEPTION: main
06-08 13:16:06.731 1520 1520 E AndroidRuntime: Process: com.example.phone, PID: 1520
06-08 13:16:06.731 1520 1520 E AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=1, result=-1, data=Intent dat=content://com.android.contacts/contacts/lookup/0r1-3B31315329433D5141294B41/1 flg=0x1 to activity com.example.phone/com.example.phone.PhoneActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at android.app.ActivityThread.-wrap16(ActivityThread.java)
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-08 13:16:06.731 1520 1520 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference
06-08 13:16:06.731 1520 1520 E AndroidRuntime: at com.example.phone.PhoneActivity.retrieveContactPhoto(PhoneActivity.java:67
来自 Github 的推荐链接:https://gist.github.com/evandrix/7058235 有人可以帮忙吗?提前致谢..
【问题讨论】:
不要在主线程上做这个工作。使用不同的线程。充分利用异步任务。 @ShubhamChauhan 我认为仅选择一个联系人不需要异步任务。问题是 java.lang.NullPointerException:尝试调用虚拟方法 'void java.io.InputStream.close()'空对象引用 【参考方案1】:我认为错误就在这里:
try
InputStream inputStream = ...
assert inputStream != null; // here
inputStream.close();
catch (IOException e)
e.printStackTrace();
尝试在 oncreate() 之前用所有其他变量声明InputStream inputStream;
。
【讨论】:
@user3357003 首先使用AsyncTask。您正在使 UI 线程变得繁重,可能正因为如此,您没有得到任何值。应用正在跳过您的操作。 @user3357003 看到这一行 编舞:跳过 54 帧!应用程序可能在其主线程上做了太多工作。【参考方案2】:试试这段代码
private void readContacts()
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
String emailContact = null;
String emailType = null;
String image_uri = "";
Bitmap bitmap = null;
try
if (cur.getCount() > 0)
while (cur.moveToNext())
try
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
image_uri = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
sb.append("\n Contact Name:" + name);
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] id , null);
while (pCur.moveToNext())
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = pCur.getInt(pCur.getColumnIndex(Phone.TYPE));
int label = pCur.getInt(pCur.getColumnIndex(Phone.LABEL));
String type_new = (String) Phone.getTypeLabel(getResources(), type, "");
String label_new = (String) Phone.getTypeLabel(getResources(), label, "");
sb.append("\n Phone number:" + phone);
pCur.close();
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] id , null);
while (emailCur.moveToNext())
emailContact = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
sb.append("\nEmail:" + emailContact + "Email type:" + emailType);
emailCur.close();
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[] id, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE ;
Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst())
String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
orgCur.close();
if (image_uri != null)
// System.out.println(Uri.parse(image_uri));
try
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(image_uri));
sb.append("\n Image in Bitmap:" + bitmap);
// System.out.println(bitmap);
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
sb.append("\n........................................");
catch (Exception ex)
catch (Exception ex)
【讨论】:
以上是关于从Android中的联系人列表中获取联系人的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Android 中的 Intent 中获取联系信息?