如何上传一个位图图像,使用Android的凌空库服务器? [英] How to upload a bitmap image to server using android volley library?

查看:161
本文介绍了如何上传一个位图图像,使用Android的凌空库服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何上传一个位图图像,使用Android的凌空库服务器?我试图使用Android抽射将图像上传到服务器。如果在Android的凌空抽射没有这样的选择,可以请你建议我最好的方法,使网络操作更快。 欢迎您给我发短信的链接到任何可用的在线教程有关这一主题

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

推荐答案

据我所知,排球是不是正确的选择发送数据(如图像)的大ammount的到远程服务器。无论如何,如果你想发送图像您应该扩展请求类并实现你的逻辑。 你可以采取的例子工具箱软件包中已经提供了一些课程。 否则,您可以使用HttpURLConnection类并实现你的逻辑,你首先要设置:

As far as i know, Volley isn't the right choice to send large ammount of data (like 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 example some classes already available in 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.

如果你不喜欢的HttpURLConnection可以更方便地使用Apache HTTP客户端。

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

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

然后:

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!

这篇关于如何上传一个位图图像,使用Android的凌空库服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆