How to upload image to server with Android?

Total Post:104

 1225  View(s)
Rate this:
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));
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?
  1. Post:194

    Re: How to upload image to server with Android?

    reqEntity.addPart("uploadedfile", new FileBody(new File(myImageToUpload)));

    This might be faster, and reads more easily, unless you really need the byte array.

      Modified On Apr-06-2018 12:05:44 AM