Android的 - 检索联系人照片和显示器 [英] Android - Retrieve Contact Photo and Display

查看:129
本文介绍了Android的 - 检索联系人照片和显示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个简单的应用程序,检索每个单独的接触,并显示在随着联系人的照片中的缩略图大小的图像视图的行的开始处的列表视图的姓名和电话号码。

我已成功地检索和显示的姓名和号码,但都失败了,在得到的照片显示在经历许多教程和堆栈后。

我终于滚回来只显示姓名和号码。

谁能帮助与联系人的照片,我提供了我下面的code。

在此先感谢!

---联系getter和setter方法​​--- 这种方法得到的名称和号码正确,但我不知道,如果即时通讯正确地获取照片。

 包内容;

 进口android.net.Uri;


 公共类ContactBean {
私人字符串名称;
私人字符串PHONENO;
私人乌里proPic;

公共字符串的getName(){
    返回名称;
}
公共无效setname可以(字符串名称){
    this.name =名称;
}
公共字符串getPhoneNo(){
    返回PHONENO;
}
公共无效setPhoneNo(字符串PHONENO){
    this.phoneNo = PHONENO;
}

公共乌里getPhotoUri(){
    返回this.proPic;
}

公共无效setPhotoUri(URI photoUri){
    this.proPic = proPic;
}

}
 

--- BackUpListActivity ---

 包backUpContacts;

进口java.io.IOException异常;
进口的java.io.InputStream;
进口的java.util.ArrayList;
进口java.util.Collections中;
进口了java.util.Comparator;
进口的java.util.List;

进口backUpContacts.BackUpListActivity;

进口com.example.contactflipper.R;

进口content.ContactBean;


 进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.ContentUris;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.database.Cursor;
进口android.graphics.Bitmap;
 进口android.graphics.BitmapFactory;
进口android.net.Uri;
进口android.os.Bundle;
 进口android.provider.ContactsContract;
进口android.view.View;
进口android.widget.AdapterView;
 进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.Toast;
进口android.widget.AdapterView.OnItemClickListener;

公共类BackUpListActivity扩展活动器具
OnItemClickListener {

私人的ListView ListView的;
私人列表< ContactBean>名单=新的ArrayList< ContactBean>();
私人字符串的ContactID;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.save_backups);

    ListView的=(的ListView)findViewById(R.id.list);
    listView.setOnItemClickListener(本);
    字符串image_uri =;
    点阵位图= NULL;

    光标手机= getContentResolver()查询(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,
            NULL,NULL);
    而(phones.moveToNext()){

        字符串名称=手机
                .getString(电话
                         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        字符串phoneNumber的=手机
                .getString(电话
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


        ContactBean objContact =新ContactBean();
        objContact.setName(名称);
        objContact.setPhoneNo(phoneNumber的);
        list.add(objContact);

    }
    phones.close();

    BackUpListAdapter objAdapter =新BackUpListAdapter(
            BackUpListActivity.this,R.layout.save_list_row,清单);
    listView.setAdapter(objAdapter);

    如果(空=名单及放大器;!&安培;!则为list.size()= 0){
        Collections.sort(列表中,新的比较< ContactBean>(){

            @覆盖
            公众诠释比较(ContactBean LHS,ContactBean RHS){
                返回lhs.getName()的compareTo(rhs.getName())。
            }
        });
        AlertDialog警报=新AlertDialog.Builder(
                BackUpListActivity.this).create();
        alert.setTitle();

        alert.setMessage(则为list.size()+跟找到!!!);

        alert.setButton(OK,新DialogInterface.OnClickListener(){

            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                dialog.dismiss();
            }
        });
        alert.show();

    } 其他 {
        showToast(不接触找到!!!);
    }
}

私人无效showToast(弦乐味精){
    Toast.makeText(这一点,味精,Toast.LENGTH_SHORT).show();
}


@覆盖
公共无效onItemClick(适配器视图<>列表视图,视图V,INT位置,
        长ID){
    ContactBean豆=(ContactBean)listview.getItemAtPosition(位置);
    //实现东西列出的每个项目的点击 - 豆
}

}
 

--- BackUpListAdapter ---

 包backUpContacts;

进口的java.util.ArrayList;
进口的java.util.List;

进口viewContacts.GridAdapter.ViewHolder;

进口com.example.contactflipper.R;

 进口content.ContactBean;

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

公共类BackUpListAdapter扩展ArrayAdapter< ContactBean>实现OnClickListener {

私人活动的活动;
私人列表< ContactBean>项目;
私人的ArrayList<布尔> itemChecked =新的ArrayList<布尔>();
私人诠释排;
私人ContactBean objBean;

公共BackUpListAdapter(活动的行为,诠释行名单,其中,ContactBean>项目){
    超(ACT,行项目);

    this.activity =行为;
    this.row =行;
    this.items =项目;

    的for(int i = 0; I< this.getCount();我++){
        itemChecked.add(我,FALSE); //初始化所有的物品为FALSE
    }

}

@覆盖
公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
    查看查看= convertView;
    ViewHolder持有人;
    如果(查看== NULL){
        LayoutInflater充气=(LayoutInflater)活动
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        鉴于= inflater.inflate(行,NULL);

        持有人=新ViewHolder();
        view.setTag(保持器);


    } 其他 {
        支架=(ViewHolder)view.getTag();
    }

    如果((项目== NULL)||((位置+ 1)> items.size()))
        返回查看;

    objBean = items.get(位置);

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

    如果(holder.tvname = NULL和放大器;!&安培;!空= objBean.getName()
            &功放;&安培; 。objBean.getName()修剪()长度()> 0){
        holder.tvname.setText(Html.fromHtml(objBean.getName()));
    }
    如果(holder.tvPhoneNo = NULL和放大器;!&安培;!空= objBean.getPhoneNo()
            &功放;&安培; 。objBean.getPhoneNo()修剪()长度()> 0){
        holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
    }


    最终的复选框的CBox =(复选框)view.findViewById(R.id.chkMarkContact); //你
    //复选框
    cBox.setOnClickListener(新OnClickListener(){

        公共无效的onClick(视图v){

            复选框CB =(复选框)v.findViewById(R.id.chkMarkContact);

            如果(cb.isChecked()){
                itemChecked.set(位置,真正的);
                //这里做一些操作
            }否则如果(!cb.isChecked()){
                itemChecked.set(位置,假);
                //这里做一些操作
            }
        }
    });
    cBox.setChecked(itemChecked.get(位置)); //这会选中或取消
    //复选框中的ListView
    //按其原来
    //位置和CheckBox永远
    //损失他的状态,当你
    //滚动列表项。



    返回查看;
}

公共类ViewHolder {
    公众的TextView tvname,tvPhoneNo;
    公共ImageView的ivPic;
}

@覆盖
公共无效的onClick(查看为arg0){
    // TODO自动生成方法存根

}

}
 

在上面的$ C $慈认为我需要类似这种

 如果(holder.tvPhoneNo = NULL和放大器;!&安培;!空= objBean.getPhoneNo()
            &功放;&安培; 。objBean.getPhoneNo()修剪()长度()> 0){
        holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
    }
 

除了要显示的照片。

我也已经创建XML中的一个ImageView的,并在上面code

宣布它

  holder.ivPic =(ImageView的)view.findViewById(R.id.profile_pic);
 

解决方案

您尝试获得接触(联系号码)的接触式ID,并使用Bitmapfactory脱$ C C图像$,如下面的工作code

 的InputStream的InputStream = NULL;
的String []投影=新的String [] {Contacts._ID};
乌里contactUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,Uri.en code(phnumber));
光标C = getContentResolver()查询(contactUri,投影,
    NULL,NULL,NULL);
如果(c.moveToFirst()){
    长的ContactID = c.getColumnIndex(Contacts._ID);

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

}
 

使用的InputStream得到位图

 位图位= BitmapFactory.de codeStream(InputStream的);
 holder.ivPic.setImageBitmap(位);
 

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

解决方案

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

这篇关于Android的 - 检索联系人照片和显示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆