fetch Contact Photo From Phone Number - Android Account

Android examples for Account:Contact Number

Description

fetch Contact Photo From Phone Number

Demo Code


//package com.java2s;
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.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import java.io.InputStream;

public class Main {
    public static Bitmap fetchContactPhotoFromPhoneNumber(Context context,
            String phoneNumber) {
        long id = fetchContactIdFromPhoneNumber(context, phoneNumber);
        if (id == 0)
            return null;
        else {/*from   ww  w . j av a2 s. co m*/
            Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, id);
            InputStream input = Contacts.openContactPhotoInputStream(
                    context.getContentResolver(), uri);
            return input == null ? null : BitmapFactory.decodeStream(input);
        }
    }

    public static long fetchContactIdFromPhoneNumber(Context context,
            String phoneNumber) {
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
        Cursor cursor = context.getContentResolver().query(uri,
                new String[] { PhoneLookup._ID }, null, null, null);

        long id = 0L;
        if (cursor.moveToFirst()) {
            id = cursor.getLong(cursor.getColumnIndex(PhoneLookup._ID));
        }
        cursor.close();

        return id;

    }
}

Related Tutorials