Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.InputStream;

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import android.provider.ContactsContract.Contacts;

public class Main {
    /**
     * Opens an InputStream for the person's photo and returns the photo as a
     * Bitmap. If the person's photo isn't present returns the
     * placeholderImageResource instead.
     * 
     * @param context
     *            the Context
     * @param id
     *            the id of the person
     * @param placeholderImageResource
     *            the image resource to use if the person doesn't have a photo
     * @param options
     *            the decoding options, can be set to null
     */
    public static Bitmap loadContactPhoto(Context context, Uri contactUri, int placeholderImageResource,
            BitmapFactory.Options options) {

        if (contactUri == null) {
            return loadPlaceholderPhoto(placeholderImageResource, context, options);
        }

        InputStream stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri);

        Bitmap bm = stream != null ? BitmapFactory.decodeStream(stream, null, options) : null;
        if (bm == null) {
            bm = loadPlaceholderPhoto(placeholderImageResource, context, options);
        }

        return bm;
    }

    private static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context,
            BitmapFactory.Options options) {
        if (placeholderImageResource == 0) {
            return null;
        }
        return BitmapFactory.decodeResource(context.getResources(), placeholderImageResource, options);
    }
}