Java Code Examples for android.provider.ContactsContract.Contacts#openContactPhotoInputStream()

The following examples show how to use android.provider.ContactsContract.Contacts#openContactPhotoInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContactsUtils14.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Bitmap getContactPhoto(Context ctxt, Uri uri, boolean hiRes, Integer defaultResource) {
    Bitmap img = null;
    InputStream s = Contacts.openContactPhotoInputStream(
            ctxt.getContentResolver(), uri, hiRes);
    img = BitmapFactory.decodeStream(s);

    if (img == null && defaultResource != null) {
        BitmapDrawable drawableBitmap = ((BitmapDrawable) ctxt.getResources().getDrawable(
                defaultResource));
        if (drawableBitmap != null) {
            img = drawableBitmap.getBitmap();
        }
    }
    return img;
}
 
Example 2
Source File: CalendarQueryService.java    From AndroidWearable-Samples with Apache License 2.0 6 votes vote down vote up
private static Asset getProfilePicture(ContentResolver contentResolver, Context context,
                                       long contactId) {
    if (contactId != -1) {
        // Try to retrieve the profile picture for the given contact.
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
        InputStream inputStream = Contacts.openContactPhotoInputStream(contentResolver,
                contactUri, true /*preferHighres*/);

        if (null != inputStream) {
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                if (bitmap != null) {
                    return Asset.createFromBytes(toByteArray(bitmap));
                } else {
                    Log.e(TAG, "Cannot decode profile picture for contact " + contactId);
                }
            } finally {
                closeQuietly(inputStream);
            }
        }
    }
    // Use a default background image if the user has no profile picture or there was an error.
    return getDefaultProfile(context.getResources());
}
 
Example 3
Source File: AsyncContactImageLoader.java    From emerald-dialer with GNU General Public License v3.0 5 votes vote down vote up
Drawable loadImageForContact(String lookupKey) {
	Uri contactUri = Contacts.lookupContact(mContext.getContentResolver(), Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey));
	
	if (null == contactUri) {
		return mDefaultDrawable;
	}
	
	InputStream contactImageStream = Contacts.openContactPhotoInputStream(mContext.getContentResolver(), contactUri);
	if (contactImageStream != null) {
		return Drawable.createFromStream(contactImageStream, "contact_image");
	} else {
		return mDefaultDrawable;
	}
}
 
Example 4
Source File: ContactsUtils5.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
public Bitmap getContactPhoto(Context ctxt, Uri uri, boolean hiRes, Integer defaultResource) {
    Bitmap img = null;
    InputStream s = Contacts.openContactPhotoInputStream(
            ctxt.getContentResolver(), uri);
    img = BitmapFactory.decodeStream(s);

    if (img == null && defaultResource != null) {
        BitmapDrawable drawableBitmap = ((BitmapDrawable) ctxt.getResources().getDrawable(
                defaultResource));
        if (drawableBitmap != null) {
            img = drawableBitmap.getBitmap();
        }
    }
    return img;
}
 
Example 5
Source File: BaseImageDownloader.java    From letv with Apache License 2.0 5 votes vote down vote up
protected InputStream getStreamFromContent(String imageUri, Object extra) throws FileNotFoundException {
    ContentResolver res = this.context.getContentResolver();
    Uri uri = Uri.parse(imageUri);
    if (isVideoUri(uri)) {
        Bitmap bitmap = Thumbnails.getThumbnail(res, Long.valueOf(uri.getLastPathSegment()).longValue(), 1, null);
        if (bitmap != null) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.PNG, 0, bos);
            return new ByteArrayInputStream(bos.toByteArray());
        }
    } else if (imageUri.startsWith(CONTENT_CONTACTS_URI_PREFIX)) {
        return Contacts.openContactPhotoInputStream(res, uri);
    }
    return res.openInputStream(uri);
}