Hello Friends!
Today I am going to share code for getting phone contact detail from your android phone.
It will pick all the phone number, email and profile picture also.
Just copy paste below code and enjoy..
Please comment for suggestion and help!
Thanks!
Today I am going to share code for getting phone contact detail from your android phone.
It will pick all the phone number, email and profile picture also.
Just copy paste below code and enjoy..
1)MainActivity.java
package com.example.phonecontactdetail; import java.io.FileNotFoundException; import java.io.IOException; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.MediaStore; import android.widget.TextView; public class MainActivity extends Activity { TextView textDetail; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textDetail = (TextView) findViewById(R.id.textView1); readContacts(); } public 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; if (cur.getCount() > 0) { while (cur.moveToNext()) { 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) { System.out.println("name : " + name + ", ID : " + id); 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)); sb.append("\n Phone number:" + phone); System.out.println("phone" + 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); System.out.println("Email " + emailContact + " Email Type : " + emailType); } emailCur.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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } sb.append("\n........................................"); } textDetail.setText(sb); } } }
2)activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_margin="10dp" android:text="TextView" /> </RelativeLayout>
Please comment for suggestion and help!
Thanks!