Android系列之Post图片和数据

原文地址:http://www.apkbus.com/forum.php?mod=viewthread&tid=2975&highlight=Android%E7%B3%BB%E5%88%97%E4%B9%8B

这几天一直在想Android中实现POST图片和POST数据的问题,今天终于写了一个小DOME

  1. private void imageClient(){
  2.       
  3.   //        // 隐藏title
  4.   //        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  5.   //   
  6.    //        // 设置全屏
  7.    //        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  8.           fileName = UUID.randomUUID().toString();
  9.   
  10.          try{
  11.             Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  12.              imgFile = new File(Environment.getExternalStorageDirectory(), fileName + ".jpg");
  13.              Uri outputFileUri = Uri.fromFile(imgFile);
  14.             cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
  15.              startActivityForResult(cameraIntent, 10);
  16.          } catch (Exception ex) {
  17.              ex.printStackTrace();
  18.             Log.e("EP", "" + ex.getMessage());
  19.          }
  20.      }
  21.      
  22.     @Override
  23.     protected void onActivityResult(int requestCode, int resultCode, Intent data){
  24.          // 父类方法
  25.           super.onActivityResult(requestCode, resultCode, data);
  26.          
  27.        switch (resultCode) {
  28.            case RESULT_OK:
  29.                 LogUtil.info("on Activity Result");
  30.                Bundle extras = data.getExtras();
  31.                 b = (Bitmap) extras.get("data");
  32.                
  33.              new Thread(new Runnable(){
  34.                      public void run(){
  35.                         if (b != null) {
  36.                              try {
  37.                                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + "/test.jpg"));
  38.                                  // 压缩图片
  39.                                   b.compress(CompressFormat.JPEG, 75, bos);
  40.                                 bos.flush();
  41.                                  bos.close();
  42.                              } catch (Exception e) {
  43.                                  Log.e("Exception", "file or compress exception")
  44.                                 e.printStackTrace();
  45.                              }
  46.             
  47.                          }
  48.                          imgFile = new File(android.os.Environment.getExternalStorageDirectory() + "/" +fileName + ".jpg");
  49.                         
  50.                          // ImgManager.resize(file, file, 200, "jpg");
  51.                         
  52.                         // 发送到服务器
  53.                          if(!HttpUtil.postImg("http://neil.finalist.hk/namex/namex/nclient/upload", "user", "client", imgFile))
  54.                              // 发送失败则重发一次
  55.                               HttpUtil.postImg("http://neil.finalist.hk/namex/namex/nclient/upload", "user", "client", imgFile);
  56.                          }
  57.                      }
  58.              }).start();
  59.         }
  60.     }
复制代码
发送
@SuppressWarnings( " deprecation " )
  1. public
  2. static
  3. boolean postImg(String url, String u, String c, File file){

  4. LogUtil.info(""
  5. + file.exists());



  6. PostMethod postMethod =
  7. new PostMethod(url);

  8. Part[] part =
  9. new Part[4];

  10. part[0] =
  11. new StringPart("u", u);


  12. part[1] =
  13. new StringPart("c", c);

  14. try {
  15. part[2] =
  16. new FilePart("Filedata", file);
  17. } catch (FileNotFoundException ex) {
  18. ex.printStackTrace();
  19. Log.e("file exception", ex.getMessage());
  20. }
  21. part[3] =
  22. new StringPart("flag", "image");

  23. MultipartRequestEntity mrp =
  24. new MultipartRequestEntity(part, postMethod.getParams());
  25. postMethod.setRequestEntity(mrp);
  26. org.apache.commons.httpclient.HttpClient client =
  27. new org.apache.commons.httpclient.HttpClient();
  28. client.getParams().setContentCharset("utf-8");

  29. try {
  30. client.executeMethod(postMethod);

  31. if("false".equals(postMethod.getRequestEntity().toString())){

  32. return
  33. false;
  34. }
  35. } catch (HttpException e) {
  36. e.printStackTrace();

  37. return
  38. false;
  39. } catch (IOException e) {
  40. e.printStackTrace();

  41. return
  42. false;
  43. }finally {

  44. if (client !=
  45. null) {
  46. client =
  47. null;
  48. }

  49. if (postMethod !=
  50. null) {
  51. postMethod =
  52. null;

  53. }

  54. return
  55. true;
  56. }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值