Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Get Contact Photo from phone number

Tags:

how can I get contact photo from a contact's address (phone number)?

like image 690
farhad.kargaran Avatar asked Feb 02 '15 07:02

farhad.kargaran


People also ask

How do I extract a contact photo?

For Android device follow below steps : Go to Chrome & visit https://contacts.google.com. Select the contact whose photo you want. Tap on option, select export & select Google CSV.

How do I get my contact photos from Google?

What to Know. In Gmail, select Google Apps icon > Contacts > Edit contact > Set contact photo. In Pick a photo window, select Upload photo. Edit image > Done > Save.

Do Androids have contact photos?

Once you tap on the Camera icon, you will see a popup menu providing with two options to add a picture to your Contact (See image below). You can either tap on Image to select a photo from the Image Gallery or tap on Take Picture to take a new picture using your Android Phone. 6.


2 Answers

public static Bitmap retrieveContactPhoto(Context context, String number) {
        ContentResolver contentResolver = context.getContentResolver();
        String contactId = null;
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

        String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID};

        Cursor cursor =
                contentResolver.query(
                        uri,
                        projection,
                        null,
                        null,
                        null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
            }
            cursor.close();
        }

        Bitmap photo = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.default_image);

        try {
            if(contactId != null) {
                InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
                    ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactId)));

                if (inputStream != null) {
                    photo = BitmapFactory.decodeStream(inputStream);
                }

                assert inputStream != null;
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return photo;
    }
like image 106
Sonia John Kavery Avatar answered Oct 05 '22 07:10

Sonia John Kavery


I think this vesion works better in all android versions :

public   Bitmap getContactsDetails(String address) {
    Bitmap bp = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.contact_default_picture);





    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));

    // querying contact data store
    Cursor phones = context.getContentResolver().query(contactUri, null, null, null, null);


    while (phones.moveToNext()) {
        String image_uri = phones.getString(phones.getColumnIndex(
                ContactsContract.CommonDataKinds.Phone.PHOTO_URI));

        if (image_uri != null) {

            try {
                bp = MediaStore.Images.Media
                        .getBitmap(context.getContentResolver(),
                                Uri.parse(image_uri));

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return   bp;

}
like image 29
Soufiane Avatar answered Oct 05 '22 07:10

Soufiane