使用AsyncHttpClient将原始图像数据作为POST参数

时间:2012-10-18 01:05:53

标签: java android https

我有一个简单的PHP服务,我试图从Android应用程序中获取,我想通过POST参数传递原始图像。我有一个PHP / curl脚本工作,它执行以下操作:

$url = "http://myphp.php"
$imagefilepath = 'path_to_png_file.png';
$imagedata = file_get_contents($imagefilepath);
$data = array('imagedata' => $imagedata);
// a few other fields are set into $data, but not important

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
curl_exec($ch);

我想使用AsyncHttpClient(http://loopj.com/android-async-http/)从我的Android应用程序模仿Java中的这个东西。

听起来很简单,我可以使用Java进行调用,但问题是我发送的数据在另一端不能识别为图像。但是,使用上面的PHP / Curl脚本,它可以在所有方面正常工作。

这是我的Java代码,我尝试了一些注释掉的东西:

String photoFilePath = "path_to_my_photo_on_disk.jpg";

Bitmap bm = BitmapFactory.decodeFile(photoFilePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] byteArrayPhoto = baos.toByteArray();

AsyncHttpClient client = new AsyncHttpClient(context);
RequestParams params = new RequestParams();
try {
      // THINGS I HAVE TRIED (AND FAILED):
      //params.put("imagedata", new File(photoFilePath));
      //params.put("imagedata", new ByteArrayInputStream(byteArrayPhoto), "photo.jpg");                        
      //params.put("imagedata", new String(byteArrayPhoto));
      //params.put("imagedata", new String(byteArrayPhoto, "UTF-8"));
      //params.put("imagedata", fileToString(photoFilePath));
      //params.put("imagedata", new FileInputStream(new File(photoFilePath)), "photo.jpg", "image/jpeg");
} catch (Exception e) {
      e.printStackTrace();
}

client.post(context, myURL, params, new AsyncHttpResponseHandler() {
...override methods, onSuccess() is called...
}

// for reference, for the above-called method:
private String fileToString(String filename) throws IOException
    {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        StringBuilder builder = new StringBuilder();
        String line;

        // For every line in the file, append it to the string builder
        while((line = reader.readLine()) != null)
        {
            builder.append(line);
        }

        return builder.toString();
    }
  • 请注意,在上面几条注释掉的行/尝试中,POST参数甚至没有将它传到另一侧。

我还尝试了一些其他方法将文件转换为字节数组,以及编码文件(base64),没有运气。无论出于何种原因,呼叫成功并且数据被传输,但每次,当我们尝试在服务器端打开图像时,它已损坏和/或不会作为JPG打开。我尝试过小型和大型图像文件。

我肯定已经完成了研究并尝试了许多我找到的解决方案,但似乎没有任何效果。我确定我在这里遗漏了一些明显的东西,但是有人能引导我朝着正确的方向前进吗?

任何帮助都会非常感激!!

1 个答案:

答案 0 :(得分:0)

答案很简单。事实证明,我确实需要对图像进行base64编码,因为它只是在发送到服务时以某种方式损坏。我是通过这段代码完成的:

String encodedPhotoStr = Base64.encodeToString(byteArrayPhoto, Base64.DEFAULT);
params.put("imagedata", encodedPhotoStr);

但是,为了解码这些数据,在PHP方面添加一行是至关重要的:

$raw_data_str = base64_decode($data_str_from_java)

这解决了问题,现在可以在发送后查看图像。因此无论出于何种原因,将原始二进制/图像数据从Java发送到PHP中都是行不通的(尽管curl脚本工作正常),我绝对不得不以这种方式对其进行编码和解码,以使其从Java代码中运行。