Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
Posts: 58
Registered: ‎02-14-2011
My Device: Bold 9700
My Carrier: Rogers

How to set a contact picture using a base64 encoded image string??

I am having difficulties setting a contact picture using a base64 encoded image string. Is there some encoding/decoding I need to do before calling contact.setPicture(base64String); ? 

Regular Contributor
Posts: 81
Registered: ‎03-09-2012
My Device: PlayBook & Bold 9780
My Carrier: n/a

Re: How to set a contact picture using a base64 encoded image string??

Sure you need to decode it first.

The setPicture function expects the picture in a Binary Large Object, not encoded in any form.

Developer
Posts: 58
Registered: ‎02-14-2011
My Device: Bold 9700
My Carrier: Rogers

Re: How to set a contact picture using a base64 encoded image string??

[ Edited ]

This is the code I am using to decode the base64 string. At the bottom is the section where I am setting the contact picture. It is returning a null error.

 

var keyStr = "ABCDEFGHIJKLMNOP" +
               "QRSTUVWXYZabcdef" +
               "ghijklmnopqrstuv" +
               "wxyz0123456789+/" +
               "=";

  function decode64(input) {
     var output = "";
     var chr1, chr2, chr3 = "";
     var enc1, enc2, enc3, enc4 = "";
     var i = 0;

     // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
     var base64test = /[^A-Za-z0-9\+\/\=]/g;
     if (base64test.exec(input)) {
        alert("There were invalid base64 characters in the input text.\n" +
              "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
              "Expect errors in decoding.");
     }
     input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

     do {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output = output + String.fromCharCode(chr1);

        if (enc3 != 64) {
           output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
           output = output + String.fromCharCode(chr3);
        }

        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";

     } while (i < input.length);

     return unescape(output);
  }

....
try
{
//need to unescape the slashes
var string=obj.base64.replace(/\//g,"/");							
var base64decoded = decode64(string);									contact.setPicture(base64decoded);
}catch (err){
alert("Set Picture Error: " + err);
}
....

  I am certain the decode64 function is returning a value. Can I decode using stringToBlob? if so what are the params? blackberry.utils.stringToBlob(base64, "BASE64")??

Developer
Posts: 58
Registered: ‎02-14-2011
My Device: Bold 9700
My Carrier: Rogers

Re: How to set a contact picture using a base64 encoded image string??

Or possibly blackberry.utils.stringToBlob(base64);?

I'm confused on this decoding thing. When I decode my base64 I get an output starting with PNG....is that the binary image data?
Highlighted
Developer
Posts: 58
Registered: ‎02-14-2011
My Device: Bold 9700
My Carrier: Rogers

Re: How to set a contact picture using a base64 encoded image string??

Got it sorted thanks for the info on binary image data!