Reputation: 182
I wanted some help with inserting a photo into contacts,as far as i have researched i found two ways we can insert contacts in our phone, one is starting the contacts activity of the phone, and the other is by inserting the values directly in the phone, i am using the first method, where we have to start the intent, when we start the intent i am not getting any solution to add image, i have options to add other minor details like name, work place, etc. The problem with second method is that, it doesn't let us know if the contact is already added, and this might cause an error, it might create duplicate contacts. What do you suggest i can do ? What i was doing till now is
Intent contactIntent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,ContactsContract.Contacts.CONTENT_URI);
contactIntent.setData(Uri.parse("tel:" +"+91"+mMobile));
contactIntent.putExtra(ContactsContract.Intents.Insert.NAME, name);
contactIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
contactIntent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, mobileEx);
startActivity(contactIntent);
Upvotes: 0
Views: 171
Reputation: 1694
For passing profile image via intent for contacts Editor screen, you can do something like shown below
Intent contactIntent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,ContactsContract.Contacts.CONTENT_URI);
contactIntent.setData(Uri.parse("tel:" +"+91"+mMobile));
contactIntent.putExtra(ContactsContract.Intents.Insert.NAME, name);
contactIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
contactIntent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, mobileEx);
Bitmap bit = BitmapFactory.decodeResource(getResources(), R.drawable.profile_image);
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
ContentValues row = new ContentValues();
row.put(Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
row.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bitmapToByteArray(bit));
data.add(row);
contactIntent.putParcelableArrayListExtra(Insert.DATA, data);
startActivity(contactIntent);
And logic for converting bitmap to byteArray is
private byte[] bitmapToByteArray(Bitmap bit) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
Upvotes: 2