Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Retrieve Contact Photo and Display

I am creating a simple application that retrieves the name and phone number of each individual contact and display in a list view along with the contact's photo in a thumbnail sized image-view at the beginning of the row.

I have managed to retrieve and display the name and number but have failed at getting the photo to display after going through many tutorials and stacks.

I eventually rolled it back to just displaying the name and number.

Can anyone help with the contact photo, I have provided my code below.

Thanks in advance!

---Contact getters and setters--- This method gets the name and number correctly but i am not sure if im getting the photo correctly.

 package content;

 import android.net.Uri;


 public class ContactBean {
private String name;
private String phoneNo;
private Uri proPic;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPhoneNo() {
    return phoneNo;
}
public void setPhoneNo(String phoneNo) {
    this.phoneNo = phoneNo;
}

public Uri getPhotoUri() {
    return this.proPic;
}

public void setPhotoUri(Uri photoUri) {
    this.proPic = proPic;
}

}

---BackUpListActivity---

package backUpContacts;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import backUpContacts.BackUpListActivity;

import com.example.contactflipper.R;

import content.ContactBean;


 import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.DialogInterface;
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.view.View;
import android.widget.AdapterView;
 import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BackUpListActivity extends Activity implements
OnItemClickListener {

private ListView listView;
private List<ContactBean> list = new ArrayList<ContactBean>();
private String contactID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.save_backups);

    listView = (ListView) findViewById(R.id.list);
    listView.setOnItemClickListener(this);
    String image_uri = "";
    Bitmap bitmap = null;

    Cursor phones = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,  null,
            null, null);
    while (phones.moveToNext()) {

        String name = phones
                .getString(phones
                         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        String phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


        ContactBean objContact = new ContactBean();
        objContact.setName(name);
        objContact.setPhoneNo(phoneNumber);
        list.add(objContact);

    }
    phones.close();

    BackUpListAdapter objAdapter = new BackUpListAdapter(
            BackUpListActivity.this, R.layout.save_list_row, list);
    listView.setAdapter(objAdapter);

    if (null != list && list.size() != 0) {
        Collections.sort(list, new Comparator<ContactBean>() {

            @Override
            public int compare(ContactBean lhs, ContactBean rhs) {
                return lhs.getName().compareTo(rhs.getName());
            }
        });
        AlertDialog alert = new AlertDialog.Builder(
                BackUpListActivity.this).create();
        alert.setTitle("");

        alert.setMessage(list.size() + " Contact Found!!!");

        alert.setButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alert.show();

    } else {
        showToast("No Contact Found!!!");
    }
}

private void showToast(String msg) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}


@Override
public void onItemClick(AdapterView<?> listview, View v, int position,
        long id) {
    ContactBean bean = (ContactBean) listview.getItemAtPosition(position);
    //implement something on the click of each listed item - bean
}

}

---BackUpListAdapter---

package backUpContacts;

import java.util.ArrayList;
import java.util.List;

import viewContacts.GridAdapter.ViewHolder;

import com.example.contactflipper.R;

 import content.ContactBean;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
 import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class BackUpListAdapter extends ArrayAdapter<ContactBean> implements  OnClickListener{

private Activity activity;
private List<ContactBean> items;
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
private int row;
private ContactBean objBean;

public BackUpListAdapter(Activity act, int row, List<ContactBean> items) {
    super(act, row, items);

    this.activity = act;
    this.row = row;
    this.items = items;

    for (int i = 0; i < this.getCount(); i++) {
        itemChecked.add(i, false); // initializes all items value with false
    }

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(row, null);

        holder = new ViewHolder();
        view.setTag(holder);


    } else {
        holder = (ViewHolder) view.getTag();
    }

    if ((items == null) || ((position + 1) > items.size()))
        return view;

    objBean = items.get(position);

    holder.tvname = (TextView) view.findViewById(R.id.tvname);
    holder.tvPhoneNo = (TextView) view.findViewById(R.id.tvphone);
    holder.ivPic = (ImageView) view.findViewById(R.id.profile_pic);

    if (holder.tvname != null && null != objBean.getName()
            && objBean.getName().trim().length() > 0) {
        holder.tvname.setText(Html.fromHtml(objBean.getName()));
    }
    if (holder.tvPhoneNo != null && null != objBean.getPhoneNo()
            && objBean.getPhoneNo().trim().length() > 0) {
        holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
    }


    final CheckBox cBox = (CheckBox) view.findViewById(R.id.chkMarkContact); // your
    // CheckBox
    cBox.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            CheckBox cb = (CheckBox) v.findViewById(R.id.chkMarkContact);

            if (cb.isChecked()) {
                itemChecked.set(position, true);
                // do some operations here
            } else if (!cb.isChecked()) {
                itemChecked.set(position, false);
                // do some operations here
            }
        }
    });
    cBox.setChecked(itemChecked.get(position)); // this will Check or Uncheck the
    // CheckBox in ListView
    // according to their original
    // position and CheckBox never
    // loss his State when you
    // Scroll the List Items.



    return view;
}

public class ViewHolder {
    public TextView tvname, tvPhoneNo;
    public ImageView ivPic;
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}

}

In the above code i believe i need something similar to this

if (holder.tvPhoneNo != null && null != objBean.getPhoneNo()
            && objBean.getPhoneNo().trim().length() > 0) {
        holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
    }

except for the photos to be displayed.

I also have already created an imageview in the xml and declared it in the above code

  holder.ivPic = (ImageView) view.findViewById(R.id.profile_pic);
like image 652
Harry117 Avatar asked Sep 12 '13 10:09

Harry117


People also ask

Where are Android contact photos stored?

Android Internal Storage If contacts are saved in the internal storage of your Android phone, they will be stored specifically in the directory of /data/data/com. Android. providers. contacts/databases/contacts.

How do I download contact pictures on Android?

Open the contact. Under the contact name, scroll down for Google Profile. If you right click on the photo, you have the option to save or download the image.


1 Answers

You try to get the contact id of the contact(phonenumber) and use Bitmapfactory to decode the image as shown in below working code.

InputStream inputStream=null;
String[] projection = new String[]{Contacts._ID};
Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,Uri.encode(phnumber));
Cursor c = getContentResolver().query(contactUri, projection,
    null, null, null);
if (c.moveToFirst()) {
    long contactId=c.getColumnIndex(Contacts._ID);

    inputStream = Contacts.openContactPhotoInputStream(activity.getContentResolver(),
                            ContentUris.withAppendedId(Contacts.CONTENT_URI, cntID));
    c.close();

}

use the inputstream to get bitmap

Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
 holder.ivPic.setImageBitmap(bitmap);
like image 97
user755 Avatar answered Sep 27 '22 17:09

user755