【发布时间】:2015-05-29 10:52:58
【问题描述】:
我正在尝试获取联系人图像,但似乎无法使其正常工作。我已经阅读了其他问题,但没有一个能够解决我的问题。
这是我的工作,我通过以下方式检索选定的联系人:
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);
}
}
}
这是我尝试显示联系人图像的方式:
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();
}
}
正如我在getContactPhoto 方法中注意到的,inputStream 始终为空,谁能告诉我如何以正确的方式获取联系人照片?
谢谢!
【问题讨论】:
标签: android android-contentprovider android-contacts android-contentresolver