Sending images can be done using the HttpComponents libraries. Download the latest HttpClient (currently 4.0.1) binary with dependencies package and copy apache-mime4j-0.6.jar and httpmime-4.0.1.jar to your project and add them to your Java build path.

You will need to add the following imports to your class.

import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; 

Now you can create a MultipartEntity to attach an image to your POST request. The following code shows an example of how to do this:

  
public void post(String url, List<NameValuePair> nameValuePairs) {   HttpClient httpClient = new DefaultHttpClient();   HttpContext localContext = new BasicHttpContext();   HttpPost httpPost = new HttpPost(url);   try {     MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);     for(int index=0; index < nameValuePairs.size(); index++) {       if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {         // If the key equals to "image", we use FileBody to transfer the data         entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));       } else {         // Normal string data         entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));       }     }     httpPost.setEntity(entity);     HttpResponse response = httpClient.execute(httpPost, localContext);   } catch (IOException e) {     e.printStackTrace();   } }

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. ActionBar - Action View
  2. 最近阶段工作相关总结,微信支付/应用商店
  3. android 添加广告用proguard混淆后不显示
  4. Android多点触控MultiTouch浅析
  5. 用Preferences,通过xml文件跳转到另一个A
  6. android关于图片缩放
  7. Android 分析Native库的加载过程及x86系
  8. Android音乐播放器-读取SD卡中歌曲
  9. Android google Map 出现问题
  10. Android Studio快捷键提高工作效率的奇技