这几天一直在想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 }

发送

                       1         @SuppressWarnings(        "        deprecation        "        )
2 public static boolean postImg(String url, String u, String c, File file){
3 LogUtil.info( "" + file.exists());
4
5 PostMethod postMethod = new PostMethod(url);
6
7 Part[] part = new Part[ 4 ];
8 part[ 0 ] = new StringPart( " u " , u);
9 part[ 1 ] = new StringPart( " c " , c);
10 try {
11 part[ 2 ] = new FilePart( " Filedata " , file);
12 } catch (FileNotFoundException ex) {
13 ex.printStackTrace();
14 Log.e( " file exception " , ex.getMessage());
15 }
16 part[ 3 ] = new StringPart( " flag " , " image " );
17
18 MultipartRequestEntity mrp = new MultipartRequestEntity(part, postMethod.getParams());
19 postMethod.setRequestEntity(mrp);
20 org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
21 client.getParams().setContentCharset( " utf-8 " );
22 try {
23 client.executeMethod(postMethod);
24 if ( " false " .equals(postMethod.getRequestEntity().toString())){
25 return false ;
26 }
27 } catch (HttpException e) {
28 e.printStackTrace();
29 return false ;
30 } catch (IOException e) {
31 e.printStackTrace();
32 return false ;
33 } finally {
34 if (client != null ) {
35 client = null ;
36 }
37 if (postMethod != null ) {
38 postMethod = null ;
39 }
40 }
41 return true ;
42 }
43

更多相关文章

  1. Android:adb shell am命令行发送Activity/Service/Broadcast
  2. Android番外篇 LruCache缓存机制
  3. android图标的制作
  4. Android实现图片放大缩小
  5. Android(安卓)CheckBox中设置padding无效解决办法
  6. Android新版Glide的RequestManager加载管理器
  7. 《新浪微博Android客户端开发完整视频 Touch Android版》
  8. Android(安卓)Volley框架使用详解
  9. Android:ImageView

随机推荐

  1. Android的drawable文件夹的说明
  2. Android编译源码时禁用werror避免报错
  3. Android(安卓)GPS获取地理位置
  4. Other:Android绘制波浪曲线
  5. android跳转到系统应用的相关API
  6. Android(安卓)自定义控件WheelView
  7. Android(安卓)onTouchEvent和onIntercept
  8. Android(安卓)为TextView 添加边框
  9. Android(安卓)新开启Activity返回结果(发
  10. 《第一行代码--Android》读书笔记之网络