4

I've been trying to store images in mysql database by encoding them in a base64 string then passing them to a php script that in turn stores this string in a blob (I also tried text) filed. What happens is the string is sent to the php script as it is, but when stored in the database, its is stored as a completely different string.

Here is how i do it:

    Bitmap image = BitmapFactory.decodeFile("/sdcard/photo2.jpg");

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, stream);

    byte[] byteImage = stream.toByteArray();

    String s = Base64.encodeToString(byteImage, Base64.NO_WRAP | Base64.URL_SAFE);

And then the string 's' is passed to the php script (correctly as I've checked it) and the script in turn insert it in the database.

This link http://diffchecker.com/kKD4w16C contains how the original encoded string (on the left of the screen) and the string that is stored in the database (on the right of the screen).

Any ideas why this is happening and how to prevent it?

Thanks in advance.

5
  • Hey.. u dont notice, that u just cut on ~20% of string? seems like there is limit for your DB field. something like varchar(255) and u try to store 1500 length string. Commented Nov 20, 2012 at 14:48
  • @StasGrin : me too , there is no difference between your strings , i think your encodage type in your database is different , use UTF8 Commented Nov 20, 2012 at 14:49
  • @Houcine sry, i edit my original comment :) Commented Nov 20, 2012 at 14:51
  • tinyurl.com/c2e7hru - see here. i just copy the end of your DB field value and find it in "original" value Commented Nov 20, 2012 at 14:52
  • Thanks guys, I changed the field to longblob and the encoding to utf-8 and it worked. Commented Nov 20, 2012 at 15:07

1 Answer 1

2

You dont notice, that u just cut on ~20% of string? Seems like there is limit for your DB field. Something like varchar(255) and u try to store 1500 length string.

http://tinyurl.com/c2e7hru - see here. I just copy the end of your DB field value and find it in "original" value.

Also, check your encoding.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.