安卓 2.3.3
我查看了有关如何获取“联系人照片”的各种示例。但他们都没有为我工作。令人惊讶的是,我在 StackOVerFlow 上偶然发现了一个代码,我使用了它,并且可以获得“照片”并将其显示在我的 ListView 中的 ImageView 中。
我的应用场景:::
我必须从电话中获取数据(图像/照片、联系人姓名、电话号码)并将它们显示在 ListView 中(每个 Item 都使用 CustomView)
带有 ListView 的 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" >
<TextView
android:id="@+id/txtViewContactsHeader"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Your Contacts (Phone + SIM)"
android:layout_gravity="center"
android:gravity="center" />
<ListView
android:id="@+id/lv_addcontact"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center" >
<Button
android:id="@+id/btn_addcontact_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD CONTACTS" />
<Button
android:id="@+id/btn_addcontact_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<TextView
android:id="@+id/txtViewNavigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Navigation"
android:layout_gravity="center"
android:gravity="center" />
</LinearLayout>
每个 ListView 项的 XML 文件 - CustomView :::
<?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="100dp"
android:layout_margin="2dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgView_addcontact"
android:layout_width="0dip"
android:layout_height="100dp"
android:layout_weight="0.20"
android:padding="5dp"
android:src="@drawable/addcontactsmall2" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_weight="0.60"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/txtView_addcontact_contactname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Name"
android:textSize="20sp" />
<TextView
android:id="@+id/txtview_addcontact_phonenumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textSize="16sp" />
</LinearLayout>
<CheckBox
android:id="@+id/chbk_addcontact"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.10"
android:layout_gravity="center"
android:padding="5dp" />
Java 文件 - 代码 :::
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.PhoneLookup;
import android.provider.ContactsContract.Data;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Class_Add_Contact extends Activity {
ListView lv_addcontacts;
Cursor cur;
ContentResolver cr;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
readContacts();
if (!((cur.moveToFirst()) || cur.equals(null) || cur.getCount() == 0)) {
} else if (cur.getCount() > 0) {
cur.moveToFirst();
}
lv_addcontacts = (ListView) findViewById(R.id.lv_addcontact);
CustomAdapterAddContacts myad = new CustomAdapterAddContacts();
lv_addcontacts.setAdapter(myad);
}
class CustomAdapterAddContacts extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
System.out.println(cur.getCount());
return cur.getCount(); // c.getCount();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// View MyView = convertView;
ContactViewHolder contactViewHolder;
if (convertView == null) {
// Inflate the layout
LayoutInflater li = getLayoutInflater();
convertView = li.inflate(
R.layout.activity_listview_addcontact_item, null);
contactViewHolder = new ContactViewHolder();
contactViewHolder.imgContact = (ImageView) convertView
.findViewById(R.id.imgView_addcontact);
contactViewHolder.txtViewContactName = (TextView) convertView
.findViewById(R.id.txtView_addcontact_contactname);
contactViewHolder.txtViewPhoneNumber = (TextView) convertView
.findViewById(R.id.txtview_addcontact_phonenumber);
contactViewHolder.chbkAddContact = (CheckBox) convertView.findViewById(R.id.chbk_addcontact);
convertView.setTag(contactViewHolder);
} else {
contactViewHolder = (ContactViewHolder) convertView.getTag();
}
cur.moveToPosition(position);
// Add Contact Name //
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (name != null)
contactViewHolder.txtViewContactName.setText(name);
else
contactViewHolder.txtViewContactName.setText("Unknown");
// Add Phone Number //
String phoneNumber = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phoneNumber != null)
contactViewHolder.txtViewPhoneNumber.setText(phoneNumber);
else
contactViewHolder.txtViewPhoneNumber.setText("Unknown");
Uri uri = getPhotoUri(Long.parseLong(fetchContactIdFromPhoneNumber(phoneNumber)));
if (uri != null) {
contactViewHolder.imgContact.setImageURI(uri);
} else {
contactViewHolder.imgContact
.setImageResource(R.drawable.addcontactsmall2);
}
return convertView;
}
private String fetchContactIdFromPhoneNumber(String phoneNumber) {
// TODO Auto-generated method stub
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cFetch = getContentResolver().query(uri,
new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
null, null, null);
String contactId = "";
if (cFetch.moveToFirst()) {
cFetch.moveToFirst();
contactId = cFetch.getString(cFetch
.getColumnIndex(PhoneLookup._ID));
}
System.out.println(contactId);
return contactId;
}
public Uri getPhotoUri(long contactId) {
ContentResolver contentResolver = getContentResolver();
try {
Cursor cursor = contentResolver
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ contactId
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cursor != null) {
if (!cursor.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri person = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactId);
return Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
public class ContactViewHolder {
ImageView imgContact;
TextView txtViewContactName;
TextView txtViewPhoneNumber;
CheckBox chbkAddContact;
}
}
private void readContacts() {
// TODO Auto-generated method stub
cr = getContentResolver();
cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (cur != null) {
cur.close();
}
}
}
我的应用程序的屏幕截图 :::
重要说明 ::: 它对我有用。我也可以为你工作。但是,我不熟悉使用内容提供程序,因此对于查询可能没有太大帮助。SO 的专家可以处理您的查询。