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);
}
?>