Reputation: 33
Following script is supposed to return a list of contacts together with their emails, phone numbers and avatars (photos).
It works like a charm it I remove the avatar (photo) retrieval code, the moment I add it back. The script breaks. Does anyone have any experiences trying to display contact photos via phonegap?
<!DOCTYPE html>
<html>
<head>
<title>Contact Example</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
// specify contact search criteria
var options = new ContactFindOptions();
options.filter=""; // empty search string returns all contacts
options.multiple=true; // return multiple results
filter = ["displayName", "name", "phoneNumbers"];
// find contacts
navigator.contacts.find(filter, onSuccess, onError, options);
}
// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {
console.log(contacts.length);
for (var i=0; i<contacts.length; i++) {
var LI=$("#list").append('<li>'+contacts[i].displayName+'</li>');
if (contacts[i].phoneNumbers) {
for (var j=0; j<contacts[i].phoneNumbers.length; j++) {
$(LI).append('<span>'+contacts[i].phoneNumbers[j].value+'</span><Br/>');
}
}
if (contacts[i].emails) {
for (var j=0; j<contacts[i].emails.length; j++) {
$(LI).append('<span>'+contacts[i].emails[j].value+'</span><br/>');
}
}
if (contacts[i].photos) {
for (var j=0; j<contacts[i].photos.length; j++) {
alert(contacts[i].photos[j].value);
//$(LI).append('<span>'+contacts[i].photos[j].value+'</span><br/>');
}
}
/*if (contacts[i].photos) {
for (var j=0; j<contacts[i].photos.length; j++) {
$(LI).append('<img src="'+contacts[i].photos[j].value+'"/><br/>');
}
}*/
}
};
// onError: Failed to get the contacts
//
function onError(contactError) {
alert('onError!');
}
</script>
</head>
<body>
<h1>Example</h1>
<p>All Contacts in a list</p>
<ul id="list">
</ul>
</body>
Upvotes: 1
Views: 1696
Reputation: 23273
Your filter does not included the "photos" field. Without it being specified the array of photos will not be populated.
Upvotes: 2