forum

Home / DeveloperSection / Forums / How to upload image to server with Android?

How to upload image to server with Android?

marcel ethan 1760 14-Oct-2014
Here is the code.
Bitmap bm = myImageToUpload;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(myServerURL);
ByteArrayBody bab = new ByteArrayBody(data, imageName);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploadedfile", bab);
reqEntity.addPart("fileName", new StringBody("image"));
reqEntity.addPart("mimeType", new StringBody("images/jpeg"));
reqEntity.addPart("extraInfo", new StringBody(extraInfo));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);

his code uploads my image along with some other information to the server. Is there a better way to do this?
My concerns - It is slow, Sometimes takes 20 minuets to upload a large image to the server. The size of the image on the server is larger than the original.
at the very least, how can I upload the image so that the image on the server is the exact same size as the image on my android device?

Updated on 14-Oct-2014

Can you answer this question?


Answer

1 Answers

Liked By