我要制作一个电话簿,以便我访问 android 的联系表格
最佳答案
//访问联系人照片
public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
if (input == null) {
return null;
}
return BitmapFactory.decodeStream(input);
}
//调用方法为
Bitmap bitmap = loadContactPhoto(getContentResolver(), _id);
imageView.setImageBitmap(bitmap);
//获取数字
private void getAllNumbers(long id) {
//Getting numbers
Cursor phones = null;
try {
phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{Phone.NUMBER, Phone.TYPE},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
null,
null);
if(phones != null) {
while(phones.moveToNext()){
switch(phones.getInt(phones.getColumnIndex(Phone.TYPE))){
case Phone.TYPE_MOBILE :
mobilePhone = phones.getString(phones.getColumnIndex(Phone.NUMBER));
break;
case Phone.TYPE_HOME :
homePhone = phones.getString(phones.getColumnIndex(Phone.NUMBER));
break;
case Phone.TYPE_WORK :
workPhone = phones.getString(phones.getColumnIndex(Phone.NUMBER));
break;
case Phone.TYPE_OTHER : // You can store other number also
}
}
}
} catch (Exception e) {
//Handle exception
} finally {
if(!phones.isClosed() || phones != null)
phones.close();
}
}
快乐编码。
关于android - 从电话簿我如何在安卓应用程序中获取联系电话和照片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5752653/