我正在访问联系人的姓名,电话号码和照片。一切正常,但如果联系人没有分配任何照片,那么该应用程序会抛出错误并强行关闭。 我有三种方法来检索名称,数字和照片,我使用这些数据来设置2个editTexts和一个imageView。
private void retrieveContactPhoto() {
Bitmap photo = null;
try {
ImageView imageView = (ImageView) findViewById(R.id.callerPic);
imageView.setImageBitmap(photo);
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.callerPic);
imageView.setImageBitmap(photo);
}
if (inputStream == null) {
Toast.makeText(getApplicationContext(), "No profile pic found", Toast.LENGTH_LONG).show();
}
assert inputStream != null;
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void retrieveContactNumber() {
String contactNumber = null;
// getting contacts ID
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);
// Using the contact ID now we will get contact phone number
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);
EditText callerNum = (EditText) findViewById(R.id.callerNum);
callerNum.setText(contactNumber);
}
private void retrieveContactName() {
String contactName = null;
// querying contact data store
Cursor cursor = getContentResolver().query(uriContact, null, null, null, null);
if (cursor.moveToFirst()) {
// DISPLAY_NAME = The display name for the contact.
// HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number.
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
Log.d(TAG, "Contact Name: " + contactName);
EditText callerName = (EditText) findViewById(R.id.callerName);
callerName.setText(contactName);
}
所以,我的问题是如何知道联系人是否有显示图片,这样如果没有图像,我可以放置默认图像?感谢。
当我选择名为Act Broadband且没有照片或号码的联系人时会发生这种情况;
logcat的:
06-20 22:48:32.267 31991-31991/fake.call.sms.fakecallandsmspro D/MainActivity3﹕ Contact Name: Act Broadband
06-20 22:48:32.275 551-749/? W/InputMethodManagerService﹕ Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@34733acc (uid=10157 pid=31991)
06-20 22:48:32.316 31991-31991/fake.call.sms.fakecallandsmspro D/MainActivity3﹕ Contact ID: 2428
06-20 22:48:32.332 31991-31991/fake.call.sms.fakecallandsmspro D/MainActivity3﹕ Contact Phone Number: null
06-20 22:48:32.345 31991-31991/fake.call.sms.fakecallandsmspro D/AndroidRuntime﹕ Shutting down VM
06-20 22:48:32.346 31991-31991/fake.call.sms.fakecallandsmspro E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: fake.call.sms.fakecallandsmspro, PID: 31991
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/3789r3128-272B4D294943272D2927412D/2428 flg=0x1 }} to activity {fake.call.sms.fakecallandsmspro/fake.call.sms.fakecallandsmspro.MainActivity3}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference
at fake.call.sms.fakecallandsmspro.MainActivity3.retrieveContactPhoto(MainActivity3.java:124)
at fake.call.sms.fakecallandsmspro.MainActivity3.onActivityResult(MainActivity3.java:74)
at android.app.Activity.dispatchActivityResult(Activity.java:6192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-20 22:48:32.348 551-12709/? W/ActivityManager﹕ Force finishing activity 1 fake.call.sms.fakecallandsmspro/.MainActivity3
06-20 22:48:32.355 551-32557/? W/DropBoxManagerService﹕ Dropping: data_app_crash (1783 > 0 bytes)
答案 0 :(得分:1)
我会改变:
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
// ImageView imageView = (ImageView) findViewById(R.id.callerPic);
imageView.setImageBitmap(photo);
}
if (inputStream == null) {
Toast.makeText(getApplicationContext(), "No profile pic found", Toast.LENGTH_LONG).show();
}
assert inputStream != null;
inputStream.close();
到
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
// ImageView imageView = (ImageView)findViewById(R.id.callerPic);
imageView.setImageBitmap(photo);
inputStream.close();
}else{
Toast.makeText(getApplicationContext(), "No profile pic found", Toast.LENGTH_LONG).show();
}
这样,如果inputStream不为null,则只调用inputStream.close(),而不进行断言。此外,没有必要让if(inputStream != null)
后跟if(inputStream == null)
。所以我用'else'语句替换了第二个'if'语句。
请告诉我这是否有帮助!