get Contact Photo By Id - Android Account

Android examples for Account:Contact ID

Description

get Contact Photo By Id

Demo Code


import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;

public class Main{
    public static Bitmap getContactPhotoById(Context context, long id,
            int defaultPhotoId) {
        Log.d(EmailPopup.LOG_TAG, "getContactPhotoById(): " + id);
        if (id == -1) {
            return null;
        } else {/*from w  ww  .  j ava 2s  .co  m*/
            try {
                InputStream in;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    in = ContactsContract.Contacts
                            .openContactPhotoInputStream(
                                    context.getContentResolver(),
                                    ContentUris
                                            .withAppendedId(
                                                    ContactsContract.Contacts.CONTENT_URI,
                                                    id), true);
                } else {
                    in = ContactsContract.Contacts
                            .openContactPhotoInputStream(
                                    context.getContentResolver(),
                                    ContentUris
                                            .withAppendedId(
                                                    ContactsContract.Contacts.CONTENT_URI,
                                                    id));
                }
                if (in == null) {
                    return null;
                } else {
                    Bitmap photo = BitmapFactory.decodeStream(in);
                    in.close();
                    return photo;
                }
            } catch (IOException e) {
                Log.d(EmailPopup.LOG_TAG, e.toString());
                return null;
            }
        }
    }
}

Related Tutorials