user3357003 user3357003 - 6 months ago 45
Java Question

Fetching contact from Contact list in Android

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);
}



App Getting Crashed when i select contact in contacts.Problem is in returning result to Activity called.I have Posted 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



Referred Link from Github : https://gist.github.com/evandrix/7058235

can anyone help pls? Thanks in Advance..

Answer

I think error is here :

try
             {
               InputStream inputStream = ...

                    }
              assert inputStream != null; // here
              inputStream.close();

             }  catch (IOException e) {
                 e.printStackTrace();
                 } 
               }

Try to declare InputStream inputStream; before oncreate() with all other variables.