1

I'm developing an app in android which connects to a web service in php. The users can send pictures, or better said, will be able to send pictures cause I'm stuck on it, to other users.

Right now the image is sent to the web service via SOAP with type xsd:base64binary. The user take a picture and the byte array with data is converted to base64 before sending it in this way:

String img;
....
@Override
public void onPictureTaken(byte[] data, Camera camera) {
     img = Base64.encodeToString(data, Base64.NO_WRAP);         
}

In the server side, php stores img string in a mysql BLOB column.

When another user ask for the picture, the server retrieves the data from mysql in a stdClass, not only the image, also id, owner, etc... and encapsulate all this data into a JSON string with the php function json_encode. The return type for this in the WSDL file is xsd:string. Back in Android what I do is:

JSONArray array = new JSONArray(response);
JSONObject row;
String imgStr;
byte[] img;

for (int i = 0; i < array.length(); i++) {
    row = array.getJSONObject(i);
    ...
    imgStr = row.getString("image");
    ...
}

if (!TextUtils.isEmpty(imgStr)) {
    img = Base64.decode(imgStr, Base64.NO_WRAP);
}

//Insert byte[] in android SQLite
dao.insert(id, owner, img);

The table column type in SQLite is BLOB as well. The problem comes up when I get the image from SQLite and try to convert it to Bitmap object. The code for this:

...
byte[] img = cursor.getBlob(cursor.getColumnIndex(DB.M_IMG));
Bitmap bmp = BitmapFactory.decodeByteArray(img, 0, img.length);
...

The img byte array seems to be alright but bmp is always null...

Some tests I've done are getting the base64 string on the image owner device before sending it and passing it to bitmap with good results, that's why I think my problem has something to do with encoding or addition of characters by json or php, I don't know...

UPDATE: If I hardcoded the base64 string from the mysql database, no bitmap is created, if I do the same with the base64 string generated in the moment I take the picture then it works.

What am I doing wrong?

Shaeldon
  • 873
  • 4
  • 18
  • 28
josecash
  • 339
  • 4
  • 19
  • What is the image type ? is it BMP ot JPEF or PNG ? – varun May 08 '13 at 16:11
  • The picture has no format, it comes from a custom Camera Activity and the app only stores the byte array in SQLite due to client requirements – josecash May 08 '13 at 16:18
  • Look here http://stackoverflow.com/questions/6520745/why-does-bitmapfactory-decodebytearray-return-null – varun May 08 '13 at 16:24
  • Basically try copying the base64 string here and see if things work http://www.motobit.com/util/base64-decoder-encoder.asp – varun May 08 '13 at 16:25
  • I did it a few days ago, no answer for me there... – josecash May 08 '13 at 16:27
  • In motobit I just get strange chars... – josecash May 08 '13 at 16:34
  • well then your image is JPEG in format, look here http://stackoverflow.com/questions/9357668/how-to-store-image-in-sqlite-database – varun May 08 '13 at 16:37
  • That's not my case, I already get the byte array in the moment I take the picture, I never convert it to Bitmap except when I want to show it... – josecash May 08 '13 at 16:49

2 Answers2

1

After a couple of days with this problem, I have the answer, it was a MySQL issue.

Apparently MySQL BLOB type can just store up to 64Kb (a clue to find the bug is that all images take up the same space of 64 Kb...The aswer was there all along but I couldn't see it...). So the SOLUTION, in my case, has been changing BLOB type in MySQL by LONGBLOB...

Thank you all for your time.

josecash
  • 339
  • 4
  • 19
0

Though I'm still thinking over this, something struck me: why use so much, when you can simply use HttpClient, cookie and retrieve image(s) instead of converting to & fro.

Also if possible, storing Blob is not highly recommended unless your picture size is small. To me, some design changes would make whole code (server & phone) lot more manageable.

CKmum
  • 641
  • 6
  • 17
  • If I do it like this is due to client requirements, the picture can never be written in the phone storage, that's not a point I can deal with. So at last I need to store it in at least SQLite, but yes, another way could be sending bytes to the service write them to a file in the server and then download via HTTP and save the bytes in SQLite...But thats the way I've chosen – josecash May 08 '13 at 17:12