Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android Post圖片和數據

Android Post圖片和數據

編輯:初級開發

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 @Override23 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 }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved