Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to display contact data image in phonegap

i am trying to fetch the contact profile pic i.e. avatar of contact data using following code in PhoneGap for android but i am getting following url in return and i don't know how should i show it in a img tag.

Code

  var defaultImagePath ='../resources/images/default_usr.png'

 var img = contacts[i].photos  != null ? 
     contacts[i].photos[0].value : defaultImagePath;

returned url is

content://com.android.contacts/contacts/739/photo

now when eventually in a list i am trying to show it using

<img src="content://com.android.contacts/contacts/739/photo"/>

but its not displaying anything ? so how would i show the contact image ?

i have the latest PhoneGap version

i have used

window.resolveLocalFileSystemURI( contacts[i].photos[0].value , this.onResolveSuccess, this.fail);

but i am getting following run time error

JNI ERROR (app bug): attempt to use stale local reference in phonegap

even though i have specified following permissions in android

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />

UPDATE

loadContacts:function(){



    var arr = [];

    var filter = ["displayName", "name", "phoneNumbers","emails","addresses","photos"];
    var options = new ContactFindOptions();
    options.filter=""; 
    options.multiple=true;    

    navigator.contacts.find(filter,
              function(contacts) {

                for (var i = 0; i < contacts.length; i++) {
                  if (contacts[i].photos) {


                     for (var j = 0; j < contacts[i].photos.length; j++) {



                                returnValidPhoto(contacts[i].photos[j], function(answer) {

                                        console.log(answer);

                                        if(contacts[i].photos[j]!=null);
                                         contacts[i].photos[j].value=answer;


                                    });




                                 var contactData = new ContactData("Name" ,
                                                    "09090909",
                                                     contacts[i].photos[j].value);

                                arr.push(contactData);




                  }
                }

            }
            Ext.getStore('ContactStore').setData(arr);

               // document.getElementById("contactdata").innerHTML = data; 
            }, function(err) {
                alert(err);
              },options);



    }

here is the image function

 function returnValidPhoto(url,callback){

    console.log("IMAGE CALLED");
    var img = new Image();
    img.onload = function() {
    //Image is ok

        console.log("IMAGE OK");

        callback(url.value);
    };
    img.onerror = function(err) {
        //Returning a default image for users without photo 

                console.log("IMAGE FAILED");

        url.value = "/resources/images/default_usr.png";
        callback("/resources/images/default_usr.png");
    }
    img.src = url.value;
};
like image 519
Hunt Avatar asked Aug 22 '13 08:08

Hunt


2 Answers

Please try this.

     function onDeviceReady() {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
            window.resolveLocalFileSystemURI("content://com.android.contacts/contacts/739/photo", onResolveSuccess, fail);
        }

        function onFileSystemSuccess(fileSystem) {
            console.log(fileSystem.name);
        }

        function onResolveSuccess(fileEntry) {
            console.log(fileEntry.name);
        }

        function fail(evt) {
            console.log(evt.target.error.code);
        }
like image 187
Amit Prajapati Avatar answered Oct 22 '22 15:10

Amit Prajapati


I came across this issue a few days ago and finally found a workaround.

var returnValidPhoto=function(url,callback){
    var img = new Image();
    img.onload = function() {
    //Image is ok
        callback(url);
    };
    img.onerror = function(err) {
        //Returning a default image for users without photo 
        callback("image_for_no_avatar.png");
    }
    img.src = url;
};

//Example usage
returnValidphoto(contact.photos[0].value, function(answer) {
    contact.photos[0].value=answer;
}
like image 30
jdponomarev Avatar answered Oct 22 '22 14:10

jdponomarev