使用POST,php和Android上传图片

时间:2011-04-11 13:25:05

标签: java android entity

尝试使用Android中的MultipartEntity上传图像后,我收到以下错误消息:

Error: Problem with [picture] (File upload error 4)

我使用以下代码上传图片:

String url = "http://www.uploadurl.com/api/files.php";
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);

MultipartEntity multiEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Temp/" +  IMAGE_FILENAME);

ContentBody cBody = new FileBody(file);

StringBody sb1 = new StringBody(getNameFormatted());
StringBody sb2 = new StringBody(getDeviceID());

MultipartEntity multipartContent = new MultipartEntity();
multipartContent.addPart("name", sb1);
multipartContent.addPart("uuid", sb2);
multipartContent.addPart("picture", cBody);

httpPost.setEntity(multipartContent); 

HttpResponse response = httpClient.execute(httpPost, localContext);

我不确定我做错了什么,我过去只使用JSON数组作为实体的一部分使用与此非常类似的代码上传图像,而不是单独添加所有这些。我认为它与图像设置到ContentBody的方式有关吗?我真的不确定。

1 个答案:

答案 0 :(得分:0)