3

I am trying to add a contact to the phone address book.
I have been successful: I added a new contact and assigned a mobile number to it.

Now I need add a JPG I have in my resources directory to the contact as the contact photo.
I am looking for a tutorial, but can't find any.

I need to target old phones, so I need to use the old Contacts API.

Can anyone help?

ContentValues contact = new ContentValues();

contact.put(People.NAME, "testContact");
Uri insertUri = activity.getContentResolver().insert(People.CONTENT_URI, contact);

Uri phoneUri = Uri.withAppendedPath(insertUri, People.Phones.CONTENT_DIRECTORY);
contact.clear();
contact.put(People.Phones.TYPE, People.TYPE_MOBILE);
contact.put(People.NUMBER, "12128911");


updateUri = activity.getContentResolver().insert(phoneUri, contact);
Moshe Marciano
  • 2,349
  • 4
  • 33
  • 44

2 Answers2

2

I use new APi 8+, You can also use this, (For support lower version In manifest file use minSDKVersion what you want..)

And what I am doing is something like, (I am using .PNG format Bitmap)

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG , 75, stream);

operations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
           .withValue(ContactsContract.Data.RAW_CONTACT_ID, 9) // here 9 is _ID where I'm inserting image
           .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
           .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
           .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO,stream.toByteArray())
           .build());

    try {
         stream.flush();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
user370305
  • 108,599
  • 23
  • 164
  • 151
  • this is using the ContactsContract classes which are not available for old phones (< 2.1) – Moshe Marciano Dec 20 '11 at 12:54
  • Ya, I already mentioned, its from 8+, That's why I told you to use this and in manifest put minsdkversion what you want.. – user370305 Dec 20 '11 at 13:01
  • sorry I am new to Android, would that work for phones running Android 1.6, 2.1 etc .. ? I thought these libraries weren't introduced until a later version. – Moshe Marciano Dec 20 '11 at 13:09
  • Yes, you can use it, In android all new version has supporte to its old version, So just make a app with API 8. Also look at comapatibility mode of android application for how can you support it to old version. I think you have to mentioned minSDKVersion tag in your manifest file. – user370305 Dec 20 '11 at 17:25
0
final Uri uri = ContactsContract.Contacts.CONTENT_URI;
    final String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_URI
    };
    //boolean mShowInvisible = false;
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
    String[] selectionArgs = null;
    final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    m_curContacts = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_URI};
    myadapter= new MySimpleCursorAdapter(this, R.layout.list_search, m_curContacts, fields, new int []{R.id.textView1,R.id.imageView1});

please try this code may be helpful for you.