Save base64encoded image to server-side database as BLOB

Viewed 2448

I am taking a photograph from within my Android app using this code:

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){
        photo = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 40, baos);
        byte[] photoByte = baos.toByteArray();
        encodedImage = Base64.encodeToString(photoByte,Base64.DEFAULT);
    }

I am sending this String encodedImage to my PHP server through POST and receiving it to $encodedImage. I have a database where I have a field myImage of type MEDIUMBLOB. I tried to save the encodedimage in myImage but it is saved as corrupted image. I tried to save as base64_decode($encodedImage) but somehow that didn't work either.

I want three things to be done:

  • Save image to server-side database (BLOB)

  • Show image to a webpage

  • Send it back to the Android app.

I am facing issues in understanding the conversion of image required in different formats for the above tasks.

For my current project, I don't want to save my image to folder and give link to the database, so that option is closed for me.

My PHP code to save the image:

$baseImage = $_POST['baseImage'];
$blobImage = base64_decode($baseImage);
$query = "INSERT INTO `complaints`(`myImage`,...) VALUES ('$blobImage',...)"
$result = mysqli_query($conn,$query);
2 Answers
Related