我使用PHP帖子请求将Android用户拍摄的照片上传到我的共享主机。我保留了图片的文件路径,以便我可以将其解码为Bitmap,然后将其发送到服务器。
编辑:似乎问题出现在图片的大小上,如果我尝试上传大小超过800 KB的照片,我会从服务器收到以下错误:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>413 Request Entity Too Large</title>
</head><body>
<h1>Request Entity Too Large</h1>
The requested resource<br />/upload_image.php<br />
does not allow request data with GET requests, or the amount of data provided in
the request exceeds the capacity limit.
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
Android方法:
private void uploadImage() {
Bitmap bitmap = BitmapFactory.decodeFile(CURRENT_PHOTO_PATH);
//Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Log.d("Bitmap processing", "DONE!");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image", image_str));
nameValuePairs.add(new BasicNameValuePair("event_id", Integer
.toString(0)));
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
final String the_string_response = convertResponseToString(response);
Log.d("Image processing", "UPLOADED!");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(UploadImage.this,
"Ready " + the_string_response,
Toast.LENGTH_LONG).show();
}
});
} catch (final Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(UploadImage.this,
"ERROR " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
System.out.println("Error in http connection "
+ e.toString());
}
}
});
t.start();
}
我想知道如何解决这个问题? 先感谢您!