Storage of base64 encoded image from android via php in mysql

I am trying to save images in mysql database by encoding them in a base64 string and then passing them to a php script, which in turn stores this string in blob (I also tried the text). It happens that the string is sent to the PHP script as is, but when it is stored in the database, it is saved 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 tested it), and the script in turn inserts it into the database.

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

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

Thanks in advance.

+4
source share
1 answer

You don’t notice that you just cut ~ 20% of the string? There seems to be a limitation for your DB field. Something like varchar(255) and you are trying to save a string of length 1500 .

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

Also check your encoding.

+2
source

Source: https://habr.com/ru/post/1447235/


All Articles