Task
"Send an image by using an HTTP POST request on the endpoint /images with either a multipart-form-encoded, or base64 encoded data parameter."
I am using Cloudsight API for my image recognition project. Refference http://docs.cloudsight.apiary.io/#reference/0/images-collection/send-an-image-for-identification
Problem
I need to send an image from gallery encoded into base64format, but I get server error 500. I can't seem to find the problem in my code maybe the image is not encoded properly?
Code
private String uploadData(String url) {
String sResponse = "";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url+"/images");
httpPost.addHeader("Content-Type", "application/json");
httpPost.addHeader("Authorization", "CloudSight API_KEY");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
String encImage = Base64.encodeToString(data, Base64.DEFAULT);
JSONObject obj = new JSONObject();
obj.put("remote_image_url",encImage );
obj.put("locale", "en");
httpPost.setEntity(new StringEntity(obj.toString()));
HttpResponse response = httpClient.execute(httpPost, localContext);
int responseCode = response.getStatusLine().getStatusCode();
sResponse = inputStreamToString(
response.getEntity().getContent()).toString();
Log.i("ResponseString", sResponse);
Log.i("code", responseCode+"");
} catch (Exception ex) {
ex.printStackTrace();
}
return sResponse;
}
private class uploadImage1 extends AsyncTask<String, String, String>{
ProgressBar progressBar = new ProgressBar(getApplication(), null, android.R.attr.progressBarStyleSmall);
protected void onPreExecute() {
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String url = params[0];
String sResponse = uploadData(url);
return sResponse;
}
}
Edit
"Note: we recommend an image resolution no higher than 1024x, and a JPEG compression level between 5-8. We resize images internally otherwise, and it could slow down the request process."
So i need to resize my image before i send it? Can you share some example?
Main Activity
Background Activity
And php file code is