当联系人有连接时,如Whatsapp或Skype,而该联系人没有照片,则会出现Whatsapp或Skype照片。
如果接触照片没有照片,如何获得连接照片?
public byte[] getPhoto(String contactId) {
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(contactId));
Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
try
{
Cursor c = getContentResolver().query(photoUri,
new String[] {ContactsContract.Contacts.Photo.PHOTO}, null, null, null);
try {
if (c.moveToFirst()) {
final byte[] image = c.getBlob(0);
final Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
c.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return new byte[0];
}
解决了
这种方法是正确的。问题出在节目的另一部分。很抱歉给您带来不便,谢谢大家。
发布于 2017-03-21 00:21:15
首先,注意Whatsapp
照片不会出现在您的Contacts
应用程序中,它只出现在Whatsapp
应用程序中,这是因为它是存储在Whatsapp
应用程序本地的专有照片,第三方应用程序无法访问它。我不确定Skype
,但是如果您确实在Contacts
应用程序中看到了一张照片,您应该能够通过API访问它。
您发布的代码访问了联系人的缩略图大小的照片,可能联系人只有一张高分辨率的照片,而没有缩略图。
使用ContactsContract.DisplayPhoto尝试这段代码
public InputStream openDisplayPhoto(long photoFileId) {
Uri displayPhotoUri = ContentUris.withAppendedId(DisplayPhoto.CONTENT_URI, photoKey);
try {
AssetFileDescriptor fd = getContentResolver().openAssetFileDescriptor(
displayPhotoUri, "r");
return fd.createInputStream();
} catch (IOException e) {
return null;
}
}
此外,此代码将显示为联系人存储的所有照片,以及它们的RawContact
ID源:
String[] projection = new String[] { CommonDataKinds.Photo.PHOTO_FILE_ID, CommonDataKinds.Photo.PHOTO, CommonDataKinds.Photo.RAW_CONTACT_ID };
String selection = Data.MIMETYPE + "='" + CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "' AND " + Data.CONTACT_ID + "=" + contactId;
Cursor c = resolver.query(Data.CONTENT_URI, projection, selection, null, null);
while (c != null && c.moveToNext()) {
Long photoId = c.getLong(0);
boolean hasPhoto = c.isNull(1);
Long rawContactId = c.getLong(2);
Log.d(TAG, "found photo: " + photoId + ", " + rawContactId + ", " + hasPhoto);
}
https://stackoverflow.com/questions/42877552
复制相似问题