Php 无法使用android应用程序将图像保存到MySQL
我创建了一个PHP脚本,允许我在MySQL数据库中保存来自HTML页面(表单POST)的图像,做得很好,我可以看到表中的行,也可以使用另一个名为load\u image\u from_db.PHP的PHP脚本在浏览器中显示它们。然而,当谈到通过android应用程序保存图像时,有一些麻烦 我可以使用android应用程序将图像上传到MySQL数据库,图像被保存,我可以在PHPmyAdmin中的表中看到它们,但是当我尝试使用相同的php脚本load\u image\u from_db.php显示它们时,它们不会显示(显示一个小的emty矩形) 我发现,当我通过Form.html保存图像时,需要32Kio,而当我通过android应用程序保存相同的图像时,需要105kio的phpMyAdmin(所有图像大约为*3) 我将把我用来保存图像的java代码发布到MySQL数据库中:Php 无法使用android应用程序将图像保存到MySQL,php,android,Php,Android,我创建了一个PHP脚本,允许我在MySQL数据库中保存来自HTML页面(表单POST)的图像,做得很好,我可以看到表中的行,也可以使用另一个名为load\u image\u from_db.PHP的PHP脚本在浏览器中显示它们。然而,当谈到通过android应用程序保存图像时,有一些麻烦 我可以使用android应用程序将图像上传到MySQL数据库,图像被保存,我可以在PHPmyAdmin中的表中看到它们,但是当我尝试使用相同的php脚本load\u image\u from_db.php显示它
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
@Override
protected String doInBackground(Bitmap... params) {
RequestHandler rh = new RequestHandler();
Bitmap bitmap = params[0];
String uploadImage = getStringImage(bitmap);
HashMap<String, String> data = new HashMap<>();
data.put(UPLOAD_KEY, uploadImage);
String result = rh.sendPostRequest(UPLOAD_URL, data);
return result;
}
}
UploadImage ui = new UploadImage();
ui.execute(bitmap);
公共字符串getStringImage(位图bmp){
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[]imageBytes=bas.toByteArray();
字符串encodedImage=Base64.encodeToString(imageBytes,Base64.DEFAULT);
返回图像;
}
@凌驾
受保护的字符串doInBackground(位图…参数){
RequestHandler rh=新的RequestHandler();
位图位图=参数[0];
字符串uploadImage=getStringImage(位图);
HashMap数据=新的HashMap();
data.put(上传_键,上传图像);
字符串结果=rh.sendPostRequest(上传URL,数据);
返回结果;
}
}
UploadImage ui=新建UploadImage();
执行(位图);
RequestHandler类:
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = br.readLine();
} else {
response = "Error Registering";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
公共字符串sendPostRequest(字符串请求URL,
HashMap postDataParams){
网址;
字符串响应=”;
试一试{
url=新url(请求url);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
连接设置读取超时(15000);
连接设置连接超时(15000);
conn.setRequestMethod(“POST”);
conn.setDoInput(真);
连接设置输出(真);
OutputStream os=conn.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if(responseCode==HttpsURLConnection.HTTP\u确定){
BufferedReader br=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
response=br.readLine();
}否则{
response=“注册错误”;
}
}捕获(例外e){
e、 printStackTrace();
}
返回响应;
}
私有字符串getPostDataString(HashMap参数)引发UnsupportedEncodingException{
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
对于(Map.Entry:params.entrySet()){
如果(第一)
第一个=假;
其他的
结果。追加(&);
append(URLEncoder.encode(entry.getKey(),“UTF-8”);
结果。追加(“=”);
append(URLEncoder.encode(entry.getValue(),“UTF-8”);
}
返回result.toString();
}
上传_image.php
如果是PHP/MySQL问题。我的脚本是否包含错误,请您帮忙?