I am trying to make a new contact in the phone throw my app. I use this code :
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.build());
if(Build.VERSION.SDK_INT >= 11 && imageUri != null){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
CapturedImage.compress(CompressFormat.JPEG, 100, stream);
byte[] bytes = stream.toByteArray();
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, ((EditText)findViewById(R.id.ETname)).getText().toString())
.withValue(StructuredName.FAMILY_NAME, ((EditText)findViewById(R.id.ETlname)).getText().toString())
.withValue(CommonDataKinds.Email.TYPE, ((EditText)findViewById(R.id.ETmail)).getText().toString())
.withValue(Photo.PHOTO, bytes)
.build());
}
else
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, ((EditText)findViewById(R.id.ETname)).getText().toString())
.withValue(StructuredName.FAMILY_NAME, ((EditText)findViewById(R.id.ETlname)).getText().toString())
.withValue(CommonDataKinds.Email.TYPE, ((EditText)findViewById(R.id.ETmail)).getText().toString())
.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Toast.makeText(getApplicationContext(), "Contact Added Successfully", Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
now the thing is that the adding operation is ok but the email won't be set and the image too.
please help me ;)
0 comments:
Post a Comment