Wednesday, February 1, 2012

[android-developers] Problem Creating new Contact with photo

Hi,

I am trying to create a new Contact with a photo, but my code doesn´t
work properly. The thing is that the first time I execute the code the
new contact doesn´t appear on the Contacts List, but the second time I
execute it the contact appears twice and properly. I have tried the
same code but omitting the section that includes the photo and the
contact appears on the contact list fine the first time I try, so the
problem must be related to the photo inclusion.

I cannot understand why the first execution doesn´t work and the
second one does. As I said, when I create the contact for the second
time the new entrance appears twice on the contact list, with the
contact´s name, photo, and everything, so I guess the problem should
not be on the encoding.

I hope someone can help me.
Thanks a lot for yout time.

Here is my code:

..............

// Get the Contact´s Picture
int id =
cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
Uri photoUri =
ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, id);
Cursor c = getContentResolver().query(photoUri, new String[]
{ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null);

byte[] photoBytes = null;
if (c.moveToFirst()) {
photoBytes = c.getBlob(0);

..........

protected void createNewContact(String name, String mobileNumber,
String emailAddress, byte[] photo) {
ArrayList<ContentProviderOperation> ops = new
ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());

//------------------------------------------------------ Names
if(name != null)
{
ops.add(ContentProviderOperation.newInsert(
ContactsContract.Data.CONTENT_URI)

.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
0)
.withValue(ContactsContract.Data.MIMETYPE,

ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(

ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
name).build()
);
}

//------------------------------------------------------ Mobile
Number
.......

//------------------------------------------------------ Email
........

//------------------------------------------------------ Photo
if(photo != null)
{

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.DATA15,
photo)
.build());
}

// Asking the Contact provider to create a new
contact
try
{
ContentProviderResult[] result =
getContentResolver().applyBatch(ContactsContract.AUTHORITY,
ops);
}
catch (Exception e)
{
e.printStackTrace();
Log.d(TAG, "Exception: " + e.getMessage());
}

Toast.makeText(TraspasoActivity.this, "New Contact " + name +
" created !!", Toast.LENGTH_SHORT).show();
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate