0

After Using this method to encode an image:

public String getStringImage(Bitmap bmp) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 0, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

Then I upload it to the server:

        ImageString = getStringImage(bitmap);

        String content="image="+ImageString;
        final OkHttpClient client = new OkHttpClient();
        Request request;
        MediaType contentType = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
        RequestBody body = RequestBody.create(contentType, content);
        request = new Request.Builder().url(uploadURL).post(body).build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String json = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

I notice that some of the spaces are missing in the string and the picture gets corrupted after I upload it to the MySQLi Data Base. I have tried different headers but they did not work. How do I solve this problem?

Edit: This is the php file that inserts the pic to the database:

<?php

$response = array();

if (isset($_POST['image']))

{

$image = $_POST['image'];

require_once __DIR__ . '/db_connect.php';

$db = new DB_CONNECT();

$result = $db->query("INSERT INTO images(image) VALUES('$image')");

if ($result) {

$response["success"] = 1;

$response["message"] = "Product successfully created.";

echo json_encode($response);

} else {

$response["success"] = 0;

$response["message"] = "Oops! An error occurred.";

echo json_encode($response);

}

} else {

$response["success"] = 0;

$response["message"] = "missing fields";

echo json_encode($response);

} 

?>
Dennis Str
  • 29
  • 10
  • image base64 can be sometimes really long so they get truncated or are not sent completely to the server. Better option will be to use multipart request for file upload – Vivek Mishra Mar 29 '17 at 07:35
  • Try sending the image in JSON Format instead of x-www-form – Shantanu Mar 29 '17 at 07:35
  • but how do I format a ContentValue to JSON format? And how do I use the multi part request? – Dennis Str Mar 29 '17 at 07:48
  • @DeStr use this example to post content as JSON http://stackoverflow.com/questions/34179922/okhttp-post-body-as-json – Shantanu Mar 29 '17 at 07:52
  • Using json is is a strange advice. And then you have to adapt the server too. – greenapps Mar 29 '17 at 07:52
  • `base64 can be sometimes really long so they get truncated or are not sent completely`. Thst is nonsense. Please dont listen to it. – greenapps Mar 29 '17 at 07:53
  • `Later I notice that some of the spaces are missing`. What is later? In the subject you mention a database. But in your post you are not talking about one. We absolutely dont know what you are doing. We have no idea if uploading goes wrong. Or messing around with the database. Or later a download. – greenapps Mar 29 '17 at 07:56
  • I edited it sorry for not being clear. I have a MySQLi Data base and there I have a table with and id and a longBlob to save the image. I also tried longText – Dennis Str Mar 29 '17 at 07:59
  • `and the picture gets corrupted after I upload it to the MySQLi Data Base. `. What you are saying here is that the base64 string is uploaded ok. And ok before you put it in the database. Well no need to post upload code. Instead you should show the code to insert in and extract from database. – greenapps Mar 29 '17 at 08:01
  • The string is ok after I encode it and in between the state of encoded and uploaded it gets corrupted, not sure exactly where – Dennis Str Mar 29 '17 at 08:05
  • I tried the use the example @shantanu but it didn't do anything. I also posted the php file in case I missed something there. – Dennis Str Mar 29 '17 at 08:54
  • also by using the method `json.put("image",ImageString);` changes the string. If the original had only forward slashes now it just alternates(/ then \ and so on). – Dennis Str Mar 29 '17 at 09:17
  • `in between the state of encoded and uploaded it gets corrupted, not sure exactly where `. Then find out. – greenapps Mar 29 '17 at 09:20
  • @DeStr compare what string you are sending and what string you are receiving at the server, what is the difference? – Shantanu Mar 29 '17 at 09:47
  • Ex: At the beginning there is a bit in the string that is is "...\\\\\\ \\\\...". That space is gone after I upload the string. And I don't know how to check it as the string is only sent to the server when it is ok, extracted with a `_POST` in the php file and then inserted to the Data Base. I am suspecting that the problem is in the header as I have no problem downloading the image. Either that or the `_POST` changes it but I doubt it. – Dennis Str Mar 29 '17 at 13:16

0 Answers0