Skip to content

Instantly share code, notes, and snippets.

@evandrix
Created October 19, 2013 16:39

Revisions

  1. evandrix revised this gist Oct 19, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.xml
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    <!-- @res/layout/main.xml -->
    <?xml version="1.0" encoding="utf-8"?>
    <!-- @res/layout/main.xml -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
  2. evandrix created this gist Oct 19, 2013.
    19 changes: 19 additions & 0 deletions AndroidManifest.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8"/>

    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application android:label="@string/app_name">
    <activity android:name="MyActivity"
    android:label="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
    </activity>
    </application>
    </manifest>
    139 changes: 139 additions & 0 deletions MyActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,139 @@
    package com.example;

    import android.app.Activity;
    import android.content.ContentUris;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.util.Log;
    import android.view.View;
    import android.widget.ImageView;

    import java.io.IOException;
    import java.io.InputStream;

    public class MyActivity extends Activity {

    private static final String TAG = MyActivity.class.getSimpleName();
    private static final int REQUEST_CODE_PICK_CONTACTS = 1;
    private Uri uriContact;
    private String contactID; // contacts unique ID


    /**
    * 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) {

    // using native contacts selection
    // Intent.ACTION_PICK = Pick an item from the data, returning what was selected.
    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;

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

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

    }
    }
    19 changes: 19 additions & 0 deletions main.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <!-- @res/layout/main.xml -->
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Select a Contact"
    android:onClick="onClickSelectContact" />

    <ImageView android:id="@+id/img_contact"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:adjustViewBounds="true"
    android:contentDescription="Contacts Image"
    />
    </LinearLayout>