questions - Question:Insert Contact (ContactsContract) via Intent with Image (Photo)

So is there anyone, who knows how to use Intent (as in example code) and insert a Photo, which is held in Bitmap ?

Example code I do use now to start dialog intent for user to let him insert or cancel and possibly edit fields before saving:

// PrivateContactClass c;
// Bitmap photo;
Intent inOrUp = new Intent(ContactsContract.Intents.Insert.ACTION, ContactsContract.Contacts.CONTENT_URI);
inOrUp.putExtra(ContactsContract.Intents.Insert.NAME, ModelUtils.formatName(c));
inOrUp.putExtra(ContactsContract.Intents.Insert.PHONE, getPrimaryPhone());
inOrUp.putExtra(ContactsContract.Intents.Insert.TERTIARY_PHONE, c.getMobile());
inOrUp.putExtra(ContactsContract.Intents.Insert.EMAIL, c.getMail());
inOrUp.putExtra(ContactsContract.Intents.Insert.JOB_TITLE, c.getFunction());
inOrUp.putExtra(ContactsContract.Intents.Insert.NOTES, getSummary());
inOrUp.putExtra(ContactsContract.Data.IS_SUPER_PRIMARY, 1);

I have found a solution, thanks to Julien's answer

Not using only intent, as I doubt that we can pass ID of image saved by Data ContentProvider, or pass Bitmap directly within Intent.

Extends from code above
Use startActivityForResult with constant request code

// must be declared in class-context
private static final int CONTACT_SAVE_INTENT_REQUEST = 1;

Add handling result from activity started by Intent

protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    switch (requestCode) {
        if (resultCode == RESULT_OK) {

Add Method to set photo

public boolean setDisplayPhotoByRawContactId(long rawContactId, Bitmap bmp) {
     ByteArrayOutputStream stream = new ByteArrayOutputStream();
     bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
     byte[] byteArray = stream.toByteArray();
     Uri pictureUri = Uri.withAppendedPath(ContentUris.withAppendedId(RawContacts.CONTENT_URI,
             rawContactId), RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
     try {
         AssetFileDescriptor afd = getContentResolver().openAssetFileDescriptor(pictureUri, "rw");
         OutputStream os = afd.createOutputStream();
         return true;
     } catch (IOException e) {
     return false;

Add Method to search for contacts and add contact photos

private void trySetPhoto() {
    // Everything is covered in try-catch, as this method can fail on
    // low-memory or few NPE
    try {
        // We must have an phone identifier by which we search for
        // format of phone number is not relevant, as ContentProvider will
        // normalize it automatically
        if (c.getMobile() != null) {
            Uri lookup = Uri.withAppendedPath(
            Cursor c = getContentResolver().query(lookup, null, null, null,
            // Remember cursor can be null in some cases
            if (c != null) {
                // we can obtain bitmap just once
                Bitmap photo_bitmap = getPhotoBitmap();
                // if there are multiple raw contacts, we want to set the photo for all of them
                while (c.moveToNext()) {
                // remember to clean up after using cursor
    } catch (Exception e) {
        // Logging procedures
    } catch (Error e) {
        // Logging procedures

asked Sep 13, 2013 in ANDROID by rajesh
edited Sep 12, 2013
0 votes

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.