Phone Contact Image Icon - app.ChooseContact

634 views
Skip to first unread message

Mauritz Zondagh

unread,
Apr 17, 2017, 5:11:44 AM4/17/17
to DroidScript
I would like to use my phone contact name,number and image icon in my app. I use the command app.ChooseContact to retrieve the Name and Number e.g. Callback: OnPhoneChoose(name,number) as per Droidscript Sample Choose
How can i retrieve the icon image to use it in my app?

If it is not possible to use the command ChooseContact to retrieve the icon image, can you please suggest another way of doing this  (example if possible)

Many thanks

Mauritz Zondagh

unread,
Apr 19, 2017, 9:37:21 AM4/19/17
to DroidScript
It does not seem to be possible to retrieve a contact image with Droidscript Javascript?

I was thinking what else can be done to get hold of that image, and one thing that comes to mind is if i share the contact information via a vcf file with my application.
Then my application can store the image ext in a private folder for use.

Can anyone maybe help with a example on how to handle a shared vcf Contact Card file? E.g. Retrieving the relevant info and image from the file?

Thanks

Mauritz Zondagh

unread,
Apr 20, 2017, 8:38:40 AM4/20/17
to DroidScript
For the benefit of other users, i found a solution - but rather big workaround.
Refer to post https://groups.google.com/d/msg/androidscript/-2DW5oUVCTQ/dC_tFHkIAgAJ on "How to display a BASE64 encoded string JPEG image"

So basically what is do is
1. Share a Contact card (information) with my app - This is not shown in the code sample below - follow the Droidscrip Sample to do this.
2. My App receives a vcf file
3. I read the vcf file, and extract the encoded data string, and append a header, and write to a text file, for retrieval in the html file
4. Then i open a Webview, which loads a HTML file
5. In the HTML file a read this text file, and load to my webview component.

If anyone has a better idea, please post it here.

Take note : Your vcf file starts like this

BEGIN:VCARD
VERSION:2.1
N:;AA Nooddiens;;;
FN:AA Nooddiens
TEL;CELL;PREF:08384322
PHOTO;ENCODING=BASE64;JPEG:/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQE
CAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJ

Your BASE64 encoded string should look like this

data:image/png;base64,
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQE
 CAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/

So in the Javascript main file TestShare.js, i open the vcf and do a lot of cutting to get to the correct string (Remove the END:VCARD at the end of your vcf file as well)

  var vcfdata = app.ReadFile("/sdcard/Droidscript/TestShare/AA Nooddiens.vcf");
    var pos =vcfdata.search("PHOTO;ENCODING=BASE64;"); // Return -1 if no match is found
    var TempString = vcfdata.substring(pos+21);
    pos = TempString.search(":"); // Return -1 if no match is found
    TempString = TempString.substring(pos+1);
    pos =TempString.search("END:VCARD"); // Return -1 if no match is found    
    TempString = TempString.substring(0,pos-1);
    TempString = "data:image/png;base64,"+TempString;

    app.WriteFile("/sdcard/Droidscript/TestShare/Image_Data1.txt",TempString);

Then i open a webview

web = app.CreateWebView( 0.8, 0.8 );
    //web.Execute('txt("'+img+'")');
lay.AddChild( web );
web.LoadUrl( "file:sdcard/Droidscript/TestShare/Image.html","allowzoom");

In the HTML file you need to load this text file, remove lf and cr

<html>
    <head></head>
    <body>
        <canvas id="c"></canvas>
        <script type="text/javascript" src="TestShare.js"></script>
         <script type="text/javascript">
            var canvas = document.getElementById("c");
            var ctx = canvas.getContext("2d");
            var Image_Data;
            var image = new Image();
            <!--image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAHFJREFUWIXt1jsKgDAQRdF7xY25cpcWC60kioI6Fm/ahHBCMh+BRmGMnAgEWnvPpzK8dvrFCCCAcoD8og4c5Lr6WB3Q3l1TBwLYPuF3YS1gn1HphgEEEABcKERrGy0E3B0HFJg7C1N/f/kTBBBA+Vi+AMkgFEvBPD17AAAAAElFTkSuQmCC"; -->
            readTextFile("file:///sdcard/Droidscript/TestShare/Image_Data1.txt");
            image.src = Image_Data;
            image.onload = function()
            {
               ctx.drawImage(image, 0, 0);
                <!--var foo = Canvas2Image.saveAsPNG(canvas); --> 
            };
            var img = canvas.toDataURL("image/png");
    
function readTextFile(file)
{
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                Image_Data = allText;
                Image_Data.replace(/[\n\r]/g, ''); 
            }
        }
    }
    rawFile.send(null);
}
        </script>
    </body>
</html>


Hope it helps someone else, it took me a day to figure this out!!

Netpower8

unread,
Apr 20, 2017, 3:26:33 PM4/20/17
to DroidScript
I have not tired this. Just an idea.

Is it possible just make a file. Write the jpeg header then put the remaining jpeg data. Dont know if it that will work?

Mauritz Zondagh

unread,
Apr 21, 2017, 5:16:59 AM4/21/17
to DroidScript
Hi Netpower8,

I think the data i have is encoded as BASE64 ASCII string, and is not binary JPEG  pixel info data.So i do not think it will work to take that data, and a general JPEG header to make up a JPG file.

Thanks

Mauritz Zondagh

unread,
May 4, 2017, 3:39:50 PM5/4/17
to DroidScript
For the benefit of other users, i found a better way.
(Loading html as variable inside Javascript file - passing parameters ext.)
Reply all
Reply to author
Forward
0 new messages