I have a simple PHP service I’m trying to hit from an Android app, and I would like to pass a raw image via a POST param. I have a PHP/curl script working which does the following:
$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);
I want to mimic this very thing in Java from my Android app, using AsyncHttpClient (http://loopj.com/android-async-http/).
Sounds simple enough, and I can get the call working in Java, but the issue is that the data I send is not recognized on the other end as an image. With the above PHP/Curl script, however, it works fine on all fronts.
Here is my Java code, with a few commented-out things I’ve tried:
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();
}
- Note that on a few of the commented-out lines/attempts above, the POST parameter doesn’t even make it across to the other side.
I’ve also tried a few other ways to get the file into a byte-array, as well as encoding the file (base64), with no luck. For whatever reason, the call is successful and data gets transferred, but each time, when we try to open the image on the server side, it’s corrupt and/or won’t open as a JPG. I’ve tried small and large image files.
I have definitely done research and tried many solutions I have found, but nothing seems to be working. I’m sure I’m missing something obvious here, but can anyone steer me in the right direction on this?
Any help would be GREATLY appreciated!!
The answer to this was fairly simple. As it turns out, I did need to base64-encode the image, as it was simply getting corrupt somehow when sending to the service. I did this via this code:
But it was vital on the PHP side to have a line added in order to decode this data as follows:
This fixed the issue and the image is now viewable after being sent. So for whatever reason, sending raw binary/image data from java into the PHP just would not work (despite the curl script working fine), and I absolutely had to encode and decode it this way to get it to work from the Java code.