【问题标题】:Unable to post image from Android device camera using HttpClient无法使用 HttpClient 从 Android 设备摄像头发布图像
【发布时间】:2012-06-08 18:05:10
【问题描述】:

我正在尝试使用 Apache Commons HttpClient 在 Android 设备上发布由相机拍摄的图像和一些其他参数。

我的发布请求正常工作,服务器读取参数正常,但我的图片没有上传。上传由 PHP 脚本处理,我可以确认正确的 HTTP Post 将导致图像被成功上传,因为我已经使用 HTML 表单测试了 PHP 脚本。我还可以在下面的代码中确认 photo != null。

HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://myurl.com/upload.php");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

if (photo != null) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    photo.compress(CompressFormat.JPEG, 75, bos);
    byte[] data = bos.toByteArray();

    ByteArrayBody bab = new ByteArrayBody(data, "submission-1.jpg");

    reqEntity.addPart("file", bab);
}

reqEntity.addPart("date", new StringBody(date));
reqEntity.addPart("lat", new StringBody(latitude));
reqEntity.addPart("long", new StringBody(longitude));
reqEntity.addPart("name", new StringBody(name));
reqEntity.addPart("email", new StringBody(email));
reqEntity.addPart("comments", new StringBody(comments));

postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);

【问题讨论】:

  • 检查 byte[] 数据是否有内容?
  • 我刚刚验证它确实包含数据
  • @PeterWillsey:我面临同样的问题。但我没有得到 MultipartEntity ?

标签: android post httpclient android-camera


【解决方案1】:

我想通了,希望其他人会觉得这很有帮助。我所要做的就是将文件类型添加到这一行:

ByteArrayBody bab = new ByteArrayBody(data, "image/jpeg", "submission.jpg");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多