Get the InputStream object of the contact photo with given contact ID. - Android Account

Android examples for Account:Contact ID

Description

Get the InputStream object of the contact photo with given contact ID.

Demo Code


//package com.java2s;
import android.content.ContentUris;
import android.content.Context;
import android.content.res.AssetFileDescriptor;

import android.net.Uri;
import android.provider.ContactsContract;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**/*w ww  .j  a v a  2s .c  o m*/
     * Get the InputStream object of the contact photo with given contact ID.
     *
     * @param context       Context object of the caller.
     * @param contactId     Contact ID.
     * @return              InputStream object of the contact photo.
     */
    public static InputStream openDisplayPhoto(Context context,
            long contactId) {
        Uri contactUri = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI, contactId);
        Uri displayPhotoUri = Uri.withAppendedPath(contactUri,
                ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
        try {
            AssetFileDescriptor fd = context.getContentResolver()
                    .openAssetFileDescriptor(displayPhotoUri, "r");
            return fd.createInputStream();
        } catch (IOException e) {
            return null;
        }
    }
}

Related Tutorials