获取所选联系人图像-Android
我试图得到联系人的形象,但似乎我无法让这个工作。我读过其他问题,但没有一个能解决我的问题 下面是我要做的,我通过执行以下操作检索所选联系人:获取所选联系人图像-Android,android,android-contentprovider,android-contacts,android-contentresolver,Android,Android Contentprovider,Android Contacts,Android Contentresolver,我试图得到联系人的形象,但似乎我无法让这个工作。我读过其他问题,但没有一个能解决我的问题 下面是我要做的,我通过执行以下操作检索所选联系人: Intent pickContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
和检索数据:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode != 0) {
Log.d(TAG, resultCode + " result");
uriContact = intent.getData();
Log.d(TAG, resultCode + " result - " + intent.getData());
// handle the picked phone number in here.
String number = GetPhoneNumber();
getContactPhoto();
String name = getContactName();
if (contacts.size() > 0) {
for (Contact contact : contacts) {
if (contact.getContactID().equals(contactID))
return;
}
}
contacts.add(new Contact(name, contactID, number));
adapter = new ContactAdapter(getActivity(), (ArrayList<Contact>) contacts);
((ListView) view.findViewById(R.id.emergency_contact_list)).setAdapter(adapter);
view.findViewById(R.id.emergency_contact_done).setEnabled(true);
}
}
}
正如我在getContactPhoto方法inputStream中注意到的,有人能告诉我如何以propper方式获取联系人照片吗
谢谢 您可以使用此方法将联系人的照片作为字节[]加载
我也试过了,但是inputstream还是空的,我也有``但它不工作。你确定你使用的是正确的contactID吗?因为我在你的代码中没有看到你在哪里设置contactID?
private void getContactPhoto() {
Bitmap photo = null;
try {
//inputStream is always null - why so?
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getActivity().getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));
Log.d(TAG, "input stream " + inputStream);
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
ImageView imageView = (ImageView) view.findViewById(R.id.testimg);
imageView.setImageBitmap(photo);
assert inputStream != null;
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] loadIcon(Context context, long contactId, boolean highRes) {
byte[] icon = null;
// Load the icon
if (contactId <= 0) {
return icon;
}
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
try {
InputStream is = null;
is = Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri, highRes);
if (is != null) {
int iconSize = is.available();
// this is if you want to avoid loading large pictures
if (iconSize > 200 * 1024) {
Log.d(TAG, "load contact. Icon too big: " + iconSize);
} else {
icon = new byte[iconSize];
is.read(icon, 0, icon.length);
}
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return icon;
}