Hello all,
Today I am going to share the code to fetch contact details(Name,phone,pic) using Content Provider.
Overview :
To fetch phone num,name and photo ,I have made a layout having a listview named :
fetch_contacts_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF00BB" >
<TextView android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#FFFFFF"
android:text="FETCH Contacts"
android:gravity="center_vertical|center_horizontal"
android:textColor="#000000"
android:textSize="18sp"
android:id="@+id/txt_header_fetchcontactlayout"/>
<ListView
android:id="@+id/list_fetchcontactlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
->Again we have to design a listview item to show single item of listview .I have named it:
contact_listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:background="#F0F0F0">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/layout_cover">
<ImageView
android:id="@+id/icon_contactlistitem"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp" />
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/icon_contactlistitem"
android:layout_marginLeft="15dp" >
<TextView
android:id="@+id/name_contactlistitem"
android:text="test"
android:textColor="#333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
->Now let's Move to the Activity: My activity name is : FetchPhoneNumActivity.java
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListView;
import android.widget.TextView;
public class FetchPhoneNumActivity extends Activity {
ArrayList<String> mName,mPhone;
ArrayList<Bitmap> mImg;
ListView list;
TextView txt_header;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fetch_contacts_layout);
txt_header = (TextView)findViewById(R.id.txt_header_fetchcontactlayout);
list = (ListView)findViewById(R.id.list_fetchcontactlayout);
txt_header.setText("Phone Number");
readContacts();
}
public void readContacts() {
mName = new ArrayList<String>();
mPhone = new ArrayList<String>();
mImg = new ArrayList<Bitmap>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
String image_uri = "";
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID));
long _id = cur.getLong(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)
{
mName.add(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));
mPhone.add(phone);
System.out.println("phone" + phone);
}
pCur.close();
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts.
openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.
Contacts.CONTENT_URI, Long.valueOf(id)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
mImg.add(photo);
}
else
{
mImg.add(null);
}
if (inputStream != null) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
list.setAdapter(new ContactsPhoneAdapter(FetchPhoneNumActivity.this,mName,mPhone,mImg));
}
}
}
->Here is the design for ContactsPhoneAdapter.java The adapter which is used to show the items in listview :
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ContactsPhoneAdapter extends BaseAdapter{
private class ViewHolder {
public RelativeLayout lnCover;
public TextView tvName;
public TextView tvTitle,txt_before;
public ImageView img_album;
public Button btn_after;
}
private LayoutInflater mLayoutInflater = null;
private Activity activity;
private ViewHolder holder;
private String name,cat_id,str_flag="";
ArrayList<String> Showname,ShowEmail;
ArrayList<Bitmap> ShowImg;
public ContactsPhoneAdapter(Activity activity2,
ArrayList<String> showname,ArrayList<String> email, ArrayList<Bitmap> mImg) {
this.activity = activity2;
this.Showname = showname;
this.ShowEmail = email;
this.ShowImg = mImg;
mLayoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return Showname.size();
}
@Override
public Object getItem(int position) {
return Showname.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.contact_listitem, null);
holder = new ViewHolder();
holder.lnCover = (RelativeLayout)convertView.findViewById(R.id.layout_cover);
holder.tvTitle= (TextView) convertView.findViewById(R.id.name_contactlistitem);
holder.img_album = (ImageView)convertView.findViewById(R.id.icon_contactlistitem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitle.setText("Name:"+Showname.get(position)+"\n Phone:"+ShowEmail.get(position));
if (ShowImg.get(position)==null) {
holder.img_album.setImageResource(android.R.drawable.picture_frame);
}
else {
holder.img_album.setImageBitmap(ShowImg.get(position));
}
holder.lnCover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("smsto:" + ShowEmail.get(position));
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "");
activity.startActivity(intent);
}
});
return convertView;
}
}
->Next step will be add user-permission in Menifest file :
<uses-permission android:name="android.permission.READ_CONTACTS"/>
->That's all I have attached the screen shot below of this demo
Today I am going to share the code to fetch contact details(Name,phone,pic) using Content Provider.
Overview :
To fetch phone num,name and photo ,I have made a layout having a listview named :
fetch_contacts_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF00BB" >
<TextView android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#FFFFFF"
android:text="FETCH Contacts"
android:gravity="center_vertical|center_horizontal"
android:textColor="#000000"
android:textSize="18sp"
android:id="@+id/txt_header_fetchcontactlayout"/>
<ListView
android:id="@+id/list_fetchcontactlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
->Again we have to design a listview item to show single item of listview .I have named it:
contact_listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:background="#F0F0F0">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/layout_cover">
<ImageView
android:id="@+id/icon_contactlistitem"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp" />
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/icon_contactlistitem"
android:layout_marginLeft="15dp" >
<TextView
android:id="@+id/name_contactlistitem"
android:text="test"
android:textColor="#333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
->Now let's Move to the Activity: My activity name is : FetchPhoneNumActivity.java
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListView;
import android.widget.TextView;
public class FetchPhoneNumActivity extends Activity {
ArrayList<String> mName,mPhone;
ArrayList<Bitmap> mImg;
ListView list;
TextView txt_header;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fetch_contacts_layout);
txt_header = (TextView)findViewById(R.id.txt_header_fetchcontactlayout);
list = (ListView)findViewById(R.id.list_fetchcontactlayout);
txt_header.setText("Phone Number");
readContacts();
}
public void readContacts() {
mName = new ArrayList<String>();
mPhone = new ArrayList<String>();
mImg = new ArrayList<Bitmap>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
String image_uri = "";
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID));
long _id = cur.getLong(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)
{
mName.add(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));
mPhone.add(phone);
System.out.println("phone" + phone);
}
pCur.close();
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts.
openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.
Contacts.CONTENT_URI, Long.valueOf(id)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
mImg.add(photo);
}
else
{
mImg.add(null);
}
if (inputStream != null) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
list.setAdapter(new ContactsPhoneAdapter(FetchPhoneNumActivity.this,mName,mPhone,mImg));
}
}
}
->Here is the design for ContactsPhoneAdapter.java The adapter which is used to show the items in listview :
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ContactsPhoneAdapter extends BaseAdapter{
private class ViewHolder {
public RelativeLayout lnCover;
public TextView tvName;
public TextView tvTitle,txt_before;
public ImageView img_album;
public Button btn_after;
}
private LayoutInflater mLayoutInflater = null;
private Activity activity;
private ViewHolder holder;
private String name,cat_id,str_flag="";
ArrayList<String> Showname,ShowEmail;
ArrayList<Bitmap> ShowImg;
public ContactsPhoneAdapter(Activity activity2,
ArrayList<String> showname,ArrayList<String> email, ArrayList<Bitmap> mImg) {
this.activity = activity2;
this.Showname = showname;
this.ShowEmail = email;
this.ShowImg = mImg;
mLayoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return Showname.size();
}
@Override
public Object getItem(int position) {
return Showname.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.contact_listitem, null);
holder = new ViewHolder();
holder.lnCover = (RelativeLayout)convertView.findViewById(R.id.layout_cover);
holder.tvTitle= (TextView) convertView.findViewById(R.id.name_contactlistitem);
holder.img_album = (ImageView)convertView.findViewById(R.id.icon_contactlistitem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitle.setText("Name:"+Showname.get(position)+"\n Phone:"+ShowEmail.get(position));
if (ShowImg.get(position)==null) {
holder.img_album.setImageResource(android.R.drawable.picture_frame);
}
else {
holder.img_album.setImageBitmap(ShowImg.get(position));
}
holder.lnCover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("smsto:" + ShowEmail.get(position));
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "");
activity.startActivity(intent);
}
});
return convertView;
}
}
->Next step will be add user-permission in Menifest file :
<uses-permission android:name="android.permission.READ_CONTACTS"/>
->That's all I have attached the screen shot below of this demo
Fetch Name,Phone Number and Photo from contacts |
No comments:
Post a Comment