Home > DeveloperSection > Forums > How to upload image to server with Android?
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    October-14-2014 12:29 AM

 Android Android  Mobile Development 
Ratings:


 1 Reply(s)
 584  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));

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?



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-14-2014 12:30 AM

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

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

Don't want to miss updates? Please click the below button!

Follow MindStick