Reputation: 2337
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);
Upvotes: 3
Views: 4831
Reputation:
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.
Upvotes: 0
Reputation: 109237
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();
}
Upvotes: 2