我这里有这段代码,它可以获取和设置联系人的照片、所有电话号码、所有电子邮件以及预定contact_id 的所有地址。工作正常,但我相信有一个更简单的方法。任何帮助表示赞赏。谢谢
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import android.app.Activity;
import android.content.ContentUris;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class ContactDetailsTest extends Activity {
ListView contactDetails;
TextView tvContactName;
TextView tvType;
ImageView ivContactPic;
ArrayList<ContactItemDetails> details;
ContactDetailsAdapter adapter;
// Extras passed from contact name list
String contactId;
String contactName;
// Content list query args
Uri uri;
String[] projection, selectionArgs;
String selection, sortOrder;
Executor executor = Executors.newCachedThreadPool();
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactinfotest);
ivContactPic = (ImageView) findViewById(R.id.ivContactPic);
contactId = getIntent().getStringExtra("contactId");
contactName = getIntent().getStringExtra("contactName");
tvContactName = (TextView) findViewById(R.id.tvContactName);
tvContactName.setText(contactName);
tvType = (TextView)findViewById(R.id.tvDetailType);
long contact_Id = Long.parseLong(contactId);
final Bitmap my_btmp = BitmapFactory.decodeStream(openDisplayPhoto(contact_Id));
ivContactPic.setImageBitmap(my_btmp);
contactDetails = (ListView) findViewById(R.id.contactDetailsList);
details = new ArrayList<ContactItemDetails>();
adapter = new ContactDetailsAdapter(details, this);
contactDetails.setAdapter(adapter);
Runnable phoneRun = new Runnable() {
@Override
public void run() {
uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
projection = null;
selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId;
selectionArgs = null;
sortOrder = null;
Cursor phoneCursor = getContentResolver().query(uri,
projection, selection, selectionArgs, sortOrder);
if (phoneCursor != null && phoneCursor.getCount() > 0) {
handler.post(new Runnable() {
@Override
public void run() {
ContactItemDetails contactItemDetails = new ContactItemDetails();
contactItemDetails.setNumber("Phone");
details.add(contactItemDetails);
adapter.notifyDataSetChanged();
}
});
while (phoneCursor.moveToNext()) {
final ContactItemDetails contactItemDetails = new ContactItemDetails();
if (Integer
.parseInt(phoneCursor.getString(phoneCursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
String number = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phoneCursor
.getInt(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String customLabel = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
String phoneLabel = (String) ContactsContract.CommonDataKinds.Phone
.getTypeLabel(ContactDetailsTest.this
.getResources(), type, customLabel);
contactItemDetails.setNumber(number);
contactItemDetails.setType(phoneLabel);
handler.post(new Runnable() {
@Override
public void run() {
details.add(contactItemDetails);
adapter.notifyDataSetChanged();
}
});
}
}
}
if (phoneCursor != null) {
phoneCursor.close();
}
uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
projection = null;
selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + contactId;
selectionArgs = null;
sortOrder = null;
Cursor emailCursor = getContentResolver().query(uri,
projection, selection, selectionArgs, sortOrder);
if (emailCursor != null && emailCursor.getCount() > 0) {
final ContactItemDetails ItemDetails = new ContactItemDetails();
ItemDetails.setNumber("Email");
ItemDetails.setType("----------");
handler.post(new Runnable() {
@Override
public void run() {
details.add(ItemDetails);
adapter.notifyDataSetChanged();
}
});
while (emailCursor.moveToNext()) {
final ContactItemDetails contactItemDetails = new ContactItemDetails();
String eAddress = emailCursor
.getString(emailCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
int type = emailCursor
.getInt(emailCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String customLabel = emailCursor
.getString(emailCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Email.LABEL));
String emailLabel = (String) ContactsContract.CommonDataKinds.Email
.getTypeLabel(
ContactDetailsTest.this.getResources(),
type, customLabel);
contactItemDetails.setNumber(eAddress);
contactItemDetails.setType(emailLabel);
handler.post(new Runnable() {
@Override
public void run() {
details.add(contactItemDetails);
adapter.notifyDataSetChanged();
}
});
}
}
if (emailCursor != null) {
emailCursor.close();
}
uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
projection = null;
selection = ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID
+ " = " + contactId;
selectionArgs = null;
sortOrder = null;
Cursor addCursor = getContentResolver().query(uri, projection,
selection, selectionArgs, sortOrder);
if (addCursor != null && addCursor.getCount() > 0) {
handler.post(new Runnable() {
@Override
public void run() {
ContactItemDetails contactItemDetails = new ContactItemDetails();
contactItemDetails.setNumber("Address");
contactItemDetails.setType("----------");
details.add(contactItemDetails);
adapter.notifyDataSetChanged();
}
});
while (addCursor.moveToNext()) {
final ContactItemDetails contactItemDetails = new ContactItemDetails();
String address = addCursor
.getString(addCursor
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
int type = addCursor
.getInt(addCursor
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
String customLabel = addCursor
.getString(addCursor
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.LABEL));
String addLabel = (String) ContactsContract.CommonDataKinds.StructuredPostal
.getTypeLabel(
ContactDetailsTest.this.getResources(),
type, customLabel);
contactItemDetails.setNumber(address);
contactItemDetails.setType(addLabel);
handler.post(new Runnable() {
@Override
public void run() {
details.add(contactItemDetails);
adapter.notifyDataSetChanged();
}
});
}
}
if (addCursor != null) {
addCursor.close();
}
}
};
executor.execute(phoneRun);
}
public InputStream openDisplayPhoto(long contact_Id) {
contact_Id = Long.parseLong(contactId);
Uri contactUri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contact_Id);
Uri displayPhotoUri = Uri.withAppendedPath(contactUri,
ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
try {
AssetFileDescriptor fd = getContentResolver()
.openAssetFileDescriptor(displayPhotoUri, "r");
return fd.createInputStream();
} catch (IOException e) {
return null;
}
}
}