This is the method that is used to get the contact photo, number and name, but I get it from the fragment.
1 Set the resolution in the manifest.
<uses-permission android:name="android.permission.READ_CONTACTS" />
2 Declare a constant:
private static final int REQUEST_CODE_PICK_CONTACT = 1;
3 In my application, the user must select a telephone contact by clicking on the button. So in the onClick () method, I do this:
contactChooserButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACT); } });
4 Add methods to retrieve the photo, name and number:
private String retrieveContactNumber() { String contactNumber = null; // getting contacts ID Cursor cursorID = getActivity().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.e(TAG, "Contact ID: " + contactID); // Using the contact ID now we will get contact phone number Cursor cursorPhone = getActivity().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)); phoneNumber = contactNumber; } cursorPhone.close(); Log.e(TAG, "Contact Phone Number: " + contactNumber); return contactNumber; } //Retrieve name private void retrieveContactName() { String contactName = null; // querying contact data store Cursor cursor = getActivity().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)); personName = contactName; } cursor.close(); Log.e(TAG, "Contact Name: " + contactName); } //Retrieve photo (this method gets a large photo, for thumbnail follow the link below) public void retrieveContactPhoto() { Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactID)); Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO); try { AssetFileDescriptor fd = getActivity().getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r"); photoAsBitmap = BitmapFactory.decodeStream(fd.createInputStream()); } catch (IOException e) { e.printStackTrace(); } }
finally, in your onActivityForResult method do the following:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { if(requestCode == REQUEST_CODE_PICK_CONTACT && resultCode == Activity.RESULT_OK) { Log.e(TAG, "Response: " + data.toString()); uriContact = data.getData(); personPhoneTextField.setText(retrieveContactNumber());
What are these actions, look at Android: get contact details (ID, name, phone, photo) on Github