0

Sending a image to mysql using android, json and php

I was able to get my bitmap converted to a properly formatted string using

converting-images-to-json-objects

Here is the code in android

JSONObject values = new JSONObject();
            values.put(KEY_CONTRACTUUID, con.UUID);
            ...
            if (con._sig != null) {
                String encodedImage = getStringFromBitmap(con._sig);
                values.put(KEY_CONTRACTSIGIMAGE, encodedImage);


    private static String getStringFromBitmap(Bitmap bitmapPicture) {
        /*
         * This functions converts Bitmap picture to a string which can be
         * JSONified.
         */
        final int COMPRESSION_QUALITY = 100;
        String encodedImage;
        ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
        bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
                byteArrayBitmapStream);
        byte[] b = byteArrayBitmapStream.toByteArray();
        encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
        return encodedImage;
    }

now that it is in base64 and a string I need to retrieve it properly to place in my BLOB in mysql

I am not using namevalue pairs or any of that nonsense - simply send it as json and get the json string like so:

$json = json_decode($HTTP_RAW_POST_DATA,true);
echo var_dump(HTTP_RAW_POST_DATA);
...
$varsigimage = $json['sigimage'];

$formatedJSONimage = "{'sigimage': '$varsigimage'}";
var_dump($formatedJSONimage);
$sigImagedecoded = json_decode($formatedJSONimage);
var_dump($sigImagedecoded);

i need to call json_decode on the image to get it out of 64bit to place in the blob correct?

However to do this I need to use the function json_decode, but json_decode assumes I will give it a JSONObject, and since I have many more objects in my $json object, i need to recreate a single JSON object with just the image inside of it, and pass that to the json_decode but it retuns json_error of type SYNTAX

What am I doing wrong, What is the correct approach of converting the base64 string to a blob?

and yes, I am going to have the same question on getting it out of the blob back to a base64 string

1 Answer 1

1

json_decode parses a JSON string and returns an associative array, mimicking the key/value pairs in the JSON string.

It seems like you are missing another step: you need to decode the base64-encoded image string back to a bitmap. e.g. in your code:

$json = json_decode($HTTP_RAW_POST_DATA,true);
echo var_dump(HTTP_RAW_POST_DATA);
...
$varsigimage = $json['sigimage'];

$image_bitmap = base64_decode($varsigimage); // decode the string back to binary

You should now be able to save $image_bitmap as a BLOB in your database.

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

3 Comments

I actually do have that - part of that tutorial in the original post. I guess i thought there was something special i had to do with it in php. but i just dont touch it and leave it in its string save it to the blob and then send the blob back to me when i need it as is, no other encoding/decoding needed other then the on android side?
If your app is sending the image as base64 encoded string in a JSON string to your server, then your server needs to decode it back to binary, if you want to save it as a BLOB. You can of course save it directly as a base64-encoded string, and decode it on the fly if needed. Bottom line is, if you want to pass JSON between app and server, binary data has to be base64 encoded/decoded between them.
so that all appears working now - my problem arises that if I store the item as base64 in a BLOB then its format is different then if I insert a image from for instance phpmyadmin, so this is why I would want to decode it from 64 before placing it in the blob correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.