我在我的几个Android项目中使用了这块代码,并没有引起任何问题。您可以根据需要修改它。它使用HttpMime-4.1.1,它是HttpCore的一部分。希望这会为你工作,以及
/***
* Execute multi part request especially for thing related to byte[]
* @param url The destination URL
* @param byteArray The byte to upload
* @param values Additional key/value pair that you want to add
* @param fileImage The image file name
* @param imageFileKey The form key for the bytes that you are uploading
* @return
* @throws Exception
*/
public static HttpResponse executeMultipartPost(String url, byte[] byteArray,
HashMap<String, StringBody> values,
String fileImage, String imageFileKey)throws Exception {
HttpPost postRequest = new HttpPost(url);
HttpClient httpClient = new DefaultHttpClient();
Set<Map.Entry<String, StringBody>> entries = values.entrySet();
MultipartEntity multipartContent = new MultipartEntity();
for(Map.Entry<String, StringBody> current : entries) {
multipartContent.addPart(current.getKey(), current.getValue());
}
InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(byteArray),
"image/jpeg", fileImage);
multipartContent.addPart(imageFileKey, isb);
postRequest.setEntity(multipartContent);
HttpResponse res = httpClient.execute(postRequest);
return res;
}
对于上面提到的情况1,您是否正在主线程上进行联网?应用程序崩溃时是否有堆栈跟踪?图像的大小是多少? –