Android - Get Contact Photo from phone number

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

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

6条回答
  •  [愿得一人]
    2020-12-15 20:16

    This is the function:

        public static  Bitmap getContactsDetails(String address) {
            Bitmap bp = BitmapFactory.decodeResource(context.getResources(),
                             R.drawable.default_contact_photo);
            String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " = '" + address + "'"; 
            Cursor phones = context.getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, selection,
                    null, null);
            while (phones.moveToNext()) {
                String image_uri = phones.getString(phones.getColumnIndex(
    ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
    
                 if (image_uri != null) {
                     try {
                      bp = MediaStore.Images.Media
                        .getBitmap(context.getContentResolver(),
                          Uri.parse(image_uri));
    
                     } catch (FileNotFoundException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                     } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                     }
                 }  
            }
            return bp;
        }
    

提交回复
热议问题