Question

I am developing an cross-platform mobile app using Titanium Appcelerator. This app is based on Sakai, in this application i have to send image to the server.

Client side i am encoding the image with base64 encoding technique using Titanium API.

//Client-Side JavaScript Code
var selectedImageB64 = Ti.Utils.base64encode(selectedImage).toString();

Ti.Utils.base64encode API

and now I am sending this string to server and there I am decoding it,

//Server-Side Java Code
 byte[] photoData = Base64.decode(selectedImageB64);
 byte[] content = photoData;

Base64 API

now the decoded data (content) is passed to the appropriate method to save the image into the database. Till here everything is working well. Image is successfully stored in the database. The size of the original image and the image stored in the database are of equal, so that I thought this encoding and decoding process is done successfully. But when I am trying to open the image in the database the image viewer displays an error message saying "Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or you don't have the latest updates to Photo Viewer.". So what I have to do now. What exactly is the problem?

Regards..

Was it helpful?

Solution 4

I found the solution for this, actually when sending the base64 encoded data from client '+' symbols in the encoded data are being replaced by a space. So i tried replacing space with '+' sign on server side. That solves this issue.

OTHER TIPS

please try the following code:

var selectedImage=image.getImage();
var selectedImageB64=Ti.Utils.base64encode(selectedImage).getText()

works for me.

For mobile side :

var base64String = Ti.Utils.base64encode(imageView.toImage()).getText()

send base64String to server.

For Server Side :

String tempPic = (String)jsonMap.get("base64String");
byte pic[] = Base64.decodeBase64(tempPic.getBytes());

Now, Play with pic[] byte array. This code work for me.

A couple things to check:

1) Save the bytes from the server to the file system instead just to eliminate a variable (namely the db)

2) Actually print out the numeric value of say the first 10 bytes on the server side, and do the same on the client side. This is to make sure the base64 encode/decode functions are implemented correctly (or they are following the same standard).

3) I don't think you need to the toString after the base64encode, you might to a Ti.Api.Info on the object before and after to the toString

4) I would like to know more about what selectedImage object is, if its a blob object in titanium it may not be the image directly, but rather a wrapper around the image (So you maybe encoding the wrong data).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top