Code example for ContentResolver

Methods: query, openContactPhotoInputStream

1
    public ContactInfo loadContact(ContentResolver contentResolver, Uri contactUri) {
        ContactInfo contactInfo = new ContactInfo();
        long contactId = -1;
 
        // Load the display name for the specified person 
        Cursor cursor = contentResolver.query(contactUri,
                new String[]{Contacts._ID, Contacts.DISPLAY_NAME, Contacts.PHOTO_ID}, null, null, null);
        try { 
            if (cursor.moveToFirst()) {
                contactId = cursor.getLong(0);
                contactInfo.setDisplayName(cursor.getString(1));
                contactInfo.setPhotoId(cursor.getInt(2));
            } 
        } finally { 
            cursor.close();
        } 
 
        boolean modified=false;
        cursor = contentResolver.query(Phone.CONTENT_URI,
        		new String[]{Phone.NUMBER},
                Phone.TYPE + "=" + Phone.TYPE_MOBILE+" and "+Phone.CONTACT_ID + "=" + contactId, null, null);
        try { 
        	 if (cursor.moveToNext()){
        		   contactInfo.setPhoneNumber(cursor.getString(0));
        		   modified=true;
        	 } 
 
        } finally { 
            cursor.close();
        } 
        if(!modified){
        	// Load the phone number (if any). 
        	cursor = contentResolver.query(Phone.CONTENT_URI,
                new String[]{Phone.NUMBER},
                Phone.CONTACT_ID + "=" + contactId, null, Phone.IS_SUPER_PRIMARY + " DESC");
        	try { 
            	if (cursor.moveToFirst()) {
                	contactInfo.setPhoneNumber(cursor.getString(0));
            	} 
        	} finally { 
        		cursor.close();
        	} 
        } 
         
         
         
         
        contactInfo.setPicture(this.loadContactPhoto(contentResolver, contactId, contactInfo.getPhotoId()));
        return contactInfo;
    } 
     
    public Bitmap loadContactPhoto(ContentResolver cr, long  id, long photo_id) 
    { 
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
        if (input != null) 
        { 
            return BitmapFactory.decodeStream(input);
        } 
        else 
        { 
            PreyLogger.d("first try failed to load photo"); 
 
        } 
 
        byte[] photoBytes = null;
 
        Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photo_id);
 
        Cursor c = cr.query(photoUri, new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null);
 
        try  
        { 
            if (c.moveToFirst()) 
                photoBytes = c.getBlob(0);