Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android圖片上傳

android圖片上傳

編輯:關於Android編程

/** * 圖片上傳 */ public void uploadFile(Bitmap inBmp, String inCode, String inTaskNum, String inType) { // String imgs="/sdcard/welcome.jpg"; // Bitmap photo = BitmapFactory.decodeFile(imgs); ByteArrayOutputStream myStream = new ByteArrayOutputStream(); inBmp.compress( Bitmap.CompressFormat.JPEG, 50, myStream );// (0 // - // 100)壓縮文件 byte[] myCopybyte = myStream.toByteArray(); int myLength = myCopybyte.length; String myCPdata = Base64.encodeToString( myCopybyte, 0, myLength, Base64.DEFAULT ); NameValuePair myCode = new BasicNameValuePair( "o", inCode ); NameValuePair myImage = new BasicNameValuePair( "image", myCPdata ); NameValuePair myTaskNum = new BasicNameValuePair( "ti", inTaskNum ); NameValuePair myType = new BasicNameValuePair( "tp", "3" );// 配件上傳     List<NameValuePair> myParams = new ArrayList<NameValuePair>(); myParams.add( myCode ); myParams.add( myImage ); myParams.add( myTaskNum ); myParams.add( myType );     try { String myURL = Constants.picurl; String myResult = ApplyService.getJsonStringPost( myURL, myParams ); if (myResult.equals( "成功" )) { for ( int i = 0; i < recordCheckedPic; i++ ) if (i == recordCheckedPic - 1) { myHandler.sendEmptyMessage( 0 ); } else { myHandler.sendEmptyMessage( 5 ); }     } else { myHandler.sendEmptyMessage( 6 ); } } catch (Exception e) { myHandler.sendEmptyMessage( 6 ); e.printStackTrace(); } finally { myHandler.sendEmptyMessage( 9 ); }     }   //service     public String getJsonStringPost(String inUrlStr, List<NameValuePair> inParams) throws Exception { String inResult = null; Log.i( "msg", "圖片上傳url" + inUrlStr ); HttpPost myHttpRequest = new HttpPost( inUrlStr ); myHttpRequest.setEntity( (HttpEntity) new UrlEncodedFormEntity( inParams, HTTP.UTF_8 ) ); HttpResponse myHttpResponse = new DefaultHttpClient().execute( myHttpRequest ); if (myHttpResponse.getStatusLine().getStatusCode() == 200) { inResult = EntityUtils.toString( myHttpResponse.getEntity() ); } else { inResult = "5"; } return inResult; }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved