我发现我的用户在同一联系人ID下获得了多张图片。我可以通过运行下一个脚本来访问这些图像:
private void loadImages(Context context, int contactId) {
String[] selectionArgs = {String.valueOf(contactId)};
String selection = ContactsContract.Data.CONTACT_ID + "=? and " +
ContactsContract.Contacts.Photo.PHOTO + " is not null";
String[] projection = {ContactsContract.Contacts.Photo.PHOTO};
Uri contentUri = ContactsContract.Data.CONTENT_URI;
Cursor cursor = context.getContentResolver().query(
contentUri,
projection,
selection, selectionArgs,
null);
if (cursor == null) {
return;
}
while (cursor.moveToNext()) {
byte[] blob = cursor.getBlob(0);
Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
saveToFile(context, bitmap);
bitmap.recycle();
}
}
但那些只是缩略图。要获得完整的图像文件,我需要做一些这样的事情(取自android docs):
public InputStream openDisplayPhoto(long contactId) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri displayPhotoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.DISPLAY_PHOTO);
try {
AssetFileDescriptor fd =
getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
return fd.createInputStream();
} catch (IOException e) {
return null;
}
}
有关如何通过Contacts.DATA._ID获取完整尺寸图像的任何想法吗?