Android系列之Post图片和数据

时间:2022-04-17 12:14:26

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