Read Phone Contacts using
ContentProvider in Android.
Contact Name,Multiple Phone
numbers,Multiple Emails and Contact Image
ContactDetails.java:
package
com.jc.practice.model;
import android.net.Uri;
import java.util.ArrayList;
public class ContactDetails {
String name;
ArrayList<String> no;
ArrayList<String> email;
Uri uri;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getNo() {
return no;
}
public void setNo(ArrayList<String> no) {
this.no = no;
}
public ArrayList<String> getEmail() {
return email;
}
public void setEmail(ArrayList<String> email) {
this.email = email;
}
public Uri getUri() {
return uri;
}
public void setUri(Uri uri) {
this.uri = uri;
}
}
contacts_li.xml:
<?xmlversion="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#93a617"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageViewContact"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/c" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Name"
android:textColor="#bc3535"
android:textSize="16sp" />
<TextView
android:id="@+id/tvNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="No"
android:textColor="#600fb1"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/tvEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#c57b7a7a"
android:gravity="center"
android:text="Email"
android:textColor="#ffffff" />
</LinearLayout>
</android.support.v7.widget.CardView>
LoaderRcvAdapter.java:
package com.jc.practice.adapter;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jc.practice.R;
import com.jc.practice.model.ContactDetails;
import com.jc.practice.model.MyContacts;
import java.util.ArrayList;
import java.util.Objects;
import butterknife.Bind;
import butterknife.ButterKnife;
public class LoaderRcvAdapter extends RecyclerView.Adapter<LoaderRcvAdapter.LoaderHolder>
{
Context context;
ArrayList<ContactDetails> list;
public LoaderRcvAdapter(Context context, ArrayList<ContactDetails> list)
{
this.context = context;
this.list = list;
}
@Override
public LoaderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new LoaderHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_li,
parent, false));
}
@Override
public void onBindViewHolder(LoaderHolder holder, int position) {
String name = "NA";
if (list.get(position).getName() != null) {
name = list.get(position).getName();
}
holder.tvName.setText(name);
String no = "NA";
if (list.get(position).getNo() != null
&& list.get(position).getNo().size() != 0) {
no = "";
for (int i = 0; i < list.get(position).getNo().size(); i++) {
no = no + list.get(position).getNo().get(i);
if (i < list.get(position).getNo().size() - 1) {
no = no + "\n";
}
}
}
holder.tvNo.setText(no);
String email = "NA";
if (list.get(position).getEmail() != null) {
email = "";
for (int i = 0; i < list.get(position).getEmail().size(); i++) {
email = email + list.get(position).getEmail().get(i);
if (i < list.get(position).getEmail().size() - 1) {
email = email + "\n";
}
}
}
holder.tvEmail.setText(email);
if (list.get(position).getUri() != null) {
DrawableRequestBuilder<String> thumbnailRequest = Glide
.with(context)
.load(list.get(position).getUri().toString());
Glide.with(context)
.load(list.get(position).getUri().toString())
.placeholder(R.drawable.launcher) .error(R.drawable.ic_up_arrow)
.override(100, 100)
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.thumbnail(thumbnailRequest)
.into(holder.imageViewContact);
}
}
@Override
public int getItemCount() {
return list.size();
}
public class LoaderHolder extends RecyclerView.ViewHolder {
@Bind(R.id.tvName)
TextView tvName;
@Bind(R.id.tvNo)
TextView tvNo;
@Bind(R.id.tvEmail)
TextView tvEmail;
@Bind(R.id.imageViewContact)
ImageView imageViewContact;
public LoaderHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
activity_my_loader.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"
android:orientation="vertical"
tools:context="com.jc.practice.MyLoaderActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample"
android:textAllCaps="false"
android:visibility="gone"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rcvLoader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
MyLoaderActivity.java:
package com.jc.practice;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import
android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import com.jc.practice.adapter.LoaderRcvAdapter;
import com.jc.practice.model.ContactDetails;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MyLoaderActivity extends AppCompatActivity {
@Bind(R.id.button)
Button button;
@Bind(R.id.rcvLoader)
RecyclerView rcvLoader;
ArrayList<ContactDetails> contactsDetailsList;
LoaderRcvAdapter loaderRcvAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_loader);
ButterKnife.bind(this);
rcvLoader.hasFixedSize();
rcvLoader.setLayoutManager(new
LinearLayoutManager(MyLoaderActivity.this));
new MyAsynctask().execute();
}
class MyAsynctask extends AsyncTask<Void,
Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
contactsDetailsList = new ArrayList<>();
loaderRcvAdapter = new LoaderRcvAdapter(MyLoaderActivity.this, contactsDetailsList);
rcvLoader.setAdapter(loaderRcvAdapter);
}
@Override
protected Void doInBackground(Void... params) {
getContacts();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Collections.sort(contactsDetailsList, new Comparator<ContactDetails>() {
@Override
public int compare(ContactDetails lhs,
ContactDetails rhs) {
return lhs.getName().compareToIgnoreCase(rhs.getName())
}
});
loaderRcvAdapter.notifyDataSetChanged();
}
}
public void getContacts() {
Cursor mCursor =
getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (mCursor.getCount() > 0) {
while (mCursor.moveToNext()) {
ContactDetails
contactDetails = new
ContactDetails();
String mContact_Id =
mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
String mName =
mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).trim();
contactDetails.setName(mName);
String has_phone_no =
mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (has_phone_no.equals("1")) {
Cursor mPhoneCursor =
getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ "=?", new String[]{mContact_Id}, null);
if (mPhoneCursor.getCount() > 0) {
ArrayList<String> nos = new
ArrayList<>();
while (mPhoneCursor.moveToNext()) {
String mNo =
mPhoneCursor.getString(mPhoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).trim().replaceAll("[ ,-]", "").replace("+91", "");
if (!nos.contains(mNo)) {
nos.add(mNo);
}
}
contactDetails.setNo(nos);
}
mPhoneCursor.close();
}
Cursor mEmailCursor =
getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ "=?", new String[]{mContact_Id}, null);
if (mEmailCursor.getCount() > 0) {
ArrayList<String> emails = new ArrayList<>();
while (mEmailCursor.moveToNext()) {
String mEmail =
mEmailCursor.getString(mEmailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).trim();
if (!emails.contains(mEmail)) {
emails.add(mEmail);
}
}
contactDetails.setEmail(emails);
}
mEmailCursor.close();
Uri personUri =
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(mContact_Id));
Uri photoUri =
Uri.withAppendedPath(personUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
contactDetails.setUri(photoUri);
contactsDetailsList.add(contactDetails);
}
}
mCursor.close();
}
}