Need to send capture image to server in android, Image I am sending is in string, code for same:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArray = baos.toByteArray();
String strBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
Problem i am facing is strBase64 length is too large, code to send image is :
private boolean post(Context context, String message) {
HttpURLConnection urlConnection = null;
try {
URL _url = new URL(server_url);
urlConnection = (HttpURLConnection) _url.openConnection();
urlConnection.setReadTimeout(1 * 60 * 1000);
urlConnection.setConnectTimeout(1 * 60 * 1000);
urlConnection.setRequestMethod("POST");
urlConnection
.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Content-Length", message
.toString().length() + "");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(
urlConnection.getOutputStream());
byte[] bs = message.getBytes();
dos.write(bs);
dos.flush();
dos.close();
if (urlConnection.getResponseMessage().toLowerCase().equals("ok")) {
InputStream is = urlConnection.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
// responseString = b.toString();
return true;
} // data sent successfully
dos.close();
} catch (IOException ioe) {
return false;
}
return false;
}
How can I increase size of httpurlconnection so that it can accept long strings also?
Reponse Error logcat:
07-24 18:53:54.110: E/Response(14602): ResponseCode ->400
07-24 18:53:54.110: E/Response(14602): ResponseMessage ->Bad Request
Not tried myself, but you can try changing content type to "application/text"
Use this method to upload image to server. I had used this method in one of my project you can also try. In this code i had put restriction that only upto 2MB image is uploaded if size of image is greater than 2MB then i compressed the image and upload.
//This is multipartentity class
Hope it will help you. If you have any issue regarding this you can ask me. Thanks :)