Android - Recuperar foto e exibição de contatos
Estou criando um aplicativo simples que recupera o nome e o número de telefone de cada contato individual e exibe em uma visualização de lista junto com a foto do contato em uma visualização de imagem em miniatura do início da linha.
Consegui recuperar e exibir o nome e o número, mas não consegui exibir a foto depois de passar por muitos tutoriais e pilhas.
Eu acabei rolando de volta para apenas exibir o nome e o número.
Alguém pode ajudar com a foto de contato, eu forneci meu código abaixo.
Desde já, obrigado!
--- Contato getters e setters --- Este método obtém o nome e número corretamente, mas não tenho certeza se estou obtendo a foto corretamente.
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
}
}
No código acima eu acredito que eu preciso de algo semelhante a este
if (holder.tvPhoneNo != null && null != objBean.getPhoneNo()
&& objBean.getPhoneNo().trim().length() > 0) {
holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
}
exceto as fotos a serem exibidas.
Eu também já criei um imageview no xml e o declarei no código acima
holder.ivPic = (ImageView) view.findViewById(R.id.profile_pic);