2 回答
TA贡献1836条经验 获得超4个赞
我解决了base64需要将空替换为“+”的问题。
<?php
include("mysqli_connect.php");
$image= $_POST['image'];
$change = str_replace(" ","+",$image);
$a = uniqid() ;
$ImagePath = "good/$a.JPEG";
$ServerURL = "https://172.30.10.1/$ImagePath";
$InsertSQL = "INSERT INTO Photoshop(photo) VALUES('$ServerURL')" ;
if(mysqli_query($connect, $InsertSQL)){
file_put_contents($ImagePath,base64_decode($change ));
echo $image;
}else{
echo "failed";
}
mysqli_close();
?>
TA贡献1830条经验 获得超3个赞
试试这个将图像转换为base64:
Uri uri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
String encodedImageData = getEncoded64ImageStringFromBitmap(bitmap);
现在在 API 中传递数据:
JsonObject data1 = new JsonObject();
data1.addProperty("imageData", "data:image/png;base64," + encodedImageData);
现在设置 ImageBitmap:
image.setImageBitmap(bitmap);
这是方法:
private String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
byte[] byteFormat = stream.toByteArray();
// get the base 64 string
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}
对于 HttpUrlConnection 试试这个......
jsonObject = new JSONObject();
jsonObject.put("imageString", encodedImage);
String data = jsonObject.toString();
String yourURL = "yourphpfileurl";
URL url = new URL(yourURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setFixedLengthStreamingMode(data.getBytes().length);
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
OutputStream out = new BufferedOutputStream(connection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(data);
writer.flush();
writer.close();
out.close();
connection.connect();
InputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(
in, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
in.close();
String result = sb.toString();
connection.disconnect();
希望这个对你有用...
添加回答
举报