Search code examples
javajavascriptpicasa

Uploading an image to picasa using a DataURL String


I am using HTML5 to create a 'camera' application that is totally web based. (without phonegap)

I now have a DataURL that I obtained as follows

canvas.getContext("2d").drawImage(video, 0, 0, 640, 480, 0, 0, 640, 480);
var img = canvas.toDataURL("image/png");

Am I able upload this to picasa once I send this as a String to the server side?

The following code didnt work and gave me a "com.google.gdata.util.InvalidEntryException: OK Not an image." exception. (imgStr is the DataURL above)

MediaByteArraySource imageByteArr = new MediaByteArraySource(imgStr.getBytes("UTF-16LE"), "image/jpeg");
myPhoto.setMediaSource(imageByteArr);
PhotoEntry returnedPhoto = client.insert(imagePostUrl, myPhoto);

Thanks in advance

EDIT: The string looks like this "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4Xuy92ZI...."


Solution

  • Need to decode the string before it can be passed to MediaBytearraySource.

    String imgStrBase64 = req.getParameter("photo").split(",")[1];          
    byte[] dataBytes= Base64.decode(imgStrBase64);
    MediaByteArraySource imageByteArr = new MediaByteArraySource(dataBytes, "image/jpeg");
    

    Hope this helps someone.