我正在尝试创建一个可以拍照并将其发送到服务器的应用,然后将其作为blob保存到数据库。
以错误的方式输入图像数据。我正在使用assynchttp post方法 和这段代码:
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap m_bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
m_bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream);
byte[] byteArray = stream.toByteArray();
String encodedImage = Base64.encodeToString(byteArray,Base64.DEFAULT);
try {
RequestParams params = new RequestParams();
params.put("city", city2);
params.put("country", country);
params.put("langi", langi);
params.put("lati", lati);
params.put("details", details);
params.put("type", type);
params.put("ID", myID);
params.put("image", encodedImage);
AsyncHttpClient client = new AsyncHttpClient();
client.post(getResources().getString(R.string.app_server_Name)
+ "addnewplace.php", params, new TextHttpResponseHandler() {
@Override
public void onStart() {
Toast.makeText(getApplicationContext(),"saving...", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(getApplicationContext(), responseString, Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Toast.makeText(getApplicationContext(), responseString, Toast.LENGTH_SHORT).show();
}
@Override
public void onFinish() {
}
});
} catch (Exception e) {
// TODO: handle exception
} finally {
frameLayout.destroyDrawingCache();
}
}
};
这是我的PHP代码:
header("Content-Type: application/json;charset=utf-8");
$image = base64_decode($_POST['image']);
$city = $_POST['city'];
$country = $_POST['country'];
$langi = $_POST['langi'];
$lati = $_POST['lati'];
$details = $_POST['details'];
$type = $_POST['type'];
$ID = $_POST['ID'];
$latFrom = deg2rad($lati);
$lonFrom = deg2rad($langi);
$user_name = "id1015576_placeitnowapp";
$password = "place311311place";
$database = "id1015576_placeitnow";
$server = "localhost"; $conn = new mysqli($server , $user_name , $password,$database );
$conn2 = new mysqli($server , $user_name , $password,$database );
$sql1 = "INSERT
INTO `places`(`PlaceName`, `PlaceLocationLongitude`, `PlaceLocationLatitude`, `Type`, `image`, `VisitorID`)
VALUES('".$details."','".$langi."','".$lati."','".$type."','".$image."','".$ID."')";
$result2 = $conn2->query($sql1);
if ($result2) {
$response = array('status' => true );
} else {
$response = array('status' => false );
}
$myJSN = json_encode($response);
echo $myJSN;