diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index f1a6a564..78568fcf 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -24,6 +24,10 @@ package com.phonegap; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -68,7 +72,12 @@ import android.webkit.WebView; * */ public class ContactAccessorSdk5 extends ContactAccessor { - + + /** + * Keep the photo size under the 1 MB blog limit. + */ + private static final long MAX_PHOTO_SIZE = 1048576; + /** * A static map that converts the JavaScript property name to Android database column name. */ @@ -316,7 +325,7 @@ public class ContactAccessorSdk5 extends ContactAccessor { } else if (mimetype.equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) && isRequired("photos",populate)) { - photos.put(photoQuery(contactId)); + photos.put(photoQuery(c, contactId)); } } catch (JSONException e) { @@ -430,7 +439,7 @@ public class ContactAccessorSdk5 extends ContactAccessor { String key; try { - Log.d(LOG_TAG, "How many fields do we have = " + fields.length()); + //Log.d(LOG_TAG, "How many fields do we have = " + fields.length()); for (int i=0; i ops, + JSONObject photo) { + byte[] bytes = getPhotoBytes(getJsonString(photo, "value")); + ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes) + .build()); + } + + /** + * Gets the raw bytes from the supplied filename + * + * @param filename the file to read the bytes from + * @return a byte array + */ + private byte[] getPhotoBytes(String filename) { + byte[] buffer = null; + try { + File fp = new File(filename); + FileInputStream in = new FileInputStream(fp); + buffer = new byte[(int) fp.length()]; + if (fp.length() <= MAX_PHOTO_SIZE) { + in.read(buffer); + } + in.close(); + } catch (FileNotFoundException e) { + Log.e(LOG_TAG, e.getMessage(), e); + } catch (IOException e) { + Log.e(LOG_TAG, e.getMessage(), e); + } + return buffer; + } + /** * Creates a new contact and stores it in the database * @@ -1285,7 +1370,6 @@ public class ContactAccessorSdk5 extends ContactAccessor { try { JSONObject name = contact.optJSONObject("name"); String displayName = contact.getString("displayName"); - Log.d(LOG_TAG, "The passed in display name is = " + displayName); if (displayName != null || name != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) @@ -1450,6 +1534,21 @@ public class ContactAccessorSdk5 extends ContactAccessor { .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, anniversary) .build()); } + + // Add photos + JSONArray photos = null; + try { + photos = contact.getJSONArray("photos"); + if (photos != null) { + for (int i=0; i