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);
}
当我在contacts.Iblem中选择联系人时,App崩溃了。问题是将结果返回给Activity调用。我已经发布了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 ResultInfo{who=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 任何人都可以帮忙吗?在此先感谢..
答案 0 :(得分:0)
我认为错误就在这里:
try
{
InputStream inputStream = ...
}
assert inputStream != null; // here
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
尝试在oncreate()之前使用所有其他变量声明InputStream inputStream;
。
答案 1 :(得分:0)
试试这段代码
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) {
}
}