You can make some changes if you want a larger extraction .
public Bitmap retrieveContactPhoto(Context context, String number) { ContentResolver contentResolver = context.getContentResolver(); String contactId = null; Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}; Cursor cursor = contentResolver.query( uri, projection, null, null, null); if (cursor != null) { while (cursor.moveToNext()) { contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID)); } cursor.close(); } Bitmap photo = BitmapFactory.decodeResource(context.getResources(), R.mipmap.popup); try { Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(contactId)); Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO); AssetFileDescriptor fd = getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r"); InputStream inputStream=fd.createInputStream(); if (inputStream != null) { photo = BitmapFactory.decodeStream(inputStream); } assert inputStream != null; inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return photo; }
to take a reduced image
public InputStream openPhoto(long contactId) { Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY); Cursor cursor = getContentResolver().query(photoUri, new String[] {Contacts.Photo.PHOTO}, null, null, null); if (cursor == null) { return null; } try { if (cursor.moveToFirst()) { byte[] data = cursor.getBlob(0); if (data != null) { return new ByteArrayInputStream(data); } } } finally { cursor.close(); } return null; }
for more details:
Click here
Neeraj Singh Jan 03 '17 at 14:33 2017-01-03 14:33
source share