Android - Get Contact Photo from phone number

后端 未结 6 1037
失恋的感觉
失恋的感觉 2020-12-15 19:45

how can I get contact photo from a contact\'s address (phone number)?

6条回答
  •  没有蜡笔的小新
    2020-12-15 20:13

    You can make some changes if you want to get Retrieving the larger photo

     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;
        }
    

    for Retrieving the thumbnail-sized photo

     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 detail:

    Click Here

提交回复
热议问题