How to upload a bitmap image to server using android volley library?
Asked Answered
D

3

3

How to upload a bitmap image to server using android volley library ?I am trying to use android volley to upload images to server . If there is no such option in android volley ,can you please suggest me the best way to make networking actions faster. You are welcome to message me the links to any available tutorials online pertaining to this subject

Dunkin answered 14/11, 2013 at 14:53 Comment(0)
C
3

As far as I know, Volley isn't the right choice to send a large amount of data (like an image) to a remote server. Anyway, if you want to send an image you should extend Request class and implements your logic. You could take as an example some classes already available in the toolbox package. Otherwise, You can use HttpURLConnection and implement your logic, first you have to set:

con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

where boundary is a string you like. Then you have to get the output stream from connection and start writing your parts.

public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
  os.write( (delimiter + boundary + "\r\n").getBytes());
  os.write( ("Content-Disposition: form-data; name=\"" + paramName +  "\";    filename=\"" + fileName + "\"\r\n"  ).getBytes());
  os.write( ("Content-Type: application/octet-stream\r\n"  ).getBytes());
  os.write( ("Content-Transfer-Encoding: binary\r\n"  ).getBytes());
  os.write("\r\n".getBytes());

  os.write(data);

  os.write("\r\n".getBytes());
}

And so on. I wrote a tutorial about it (since you are asking a link). You can give a look here.

If you don't like HttpUrlConnection you can use more easily Apache Http client.

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost(url); 

and then:

MultipartEntity multiPart = new MultipartEntity();
multiPart.addPart("param1", new StringBody(param1)); 
multiPart.addPart("param2", new StringBody(param2)); 
multiPart.addPart("file", new ByteArrayBody(baos.toByteArray(), "logo.png")); // Your image

Hope it helps you!

Crybaby answered 14/11, 2013 at 15:40 Comment(1)
Could you be precise and tell us why you think that volley is not the right choice to send large data? (imho images are medium, not large)Romonaromonda
R
1

You could extends a subclass of Request ,and override the getBody() method, and return image's byte data in the getBody() method.

Radburn answered 23/4, 2014 at 6:35 Comment(1)
Hi @footman, do you mind to share how you got this to work? I am having issues with using Volley to post an image to a server. Always getting a 400 response. Mind to share?Electrify
M
0

Image can be sent to server using volley lib without using Multipart class. You just need to send the image in base64 format to server. It worked for me.

Mcfarland answered 22/4, 2015 at 12:48 Comment(1)
Base64 is not a great way to pass images to a web service. It only works with small images. As soon as you start working with larger images, you run in to all sorts of issues (Request Entity to large etc.)Stafani

© 2022 - 2024 — McMap. All rights reserved.