分享

Android拍照上传代码样例

 CevenCheng 2010-11-08

1、LoginWindow.java --登录窗口

 

  1. package com.hemi.rhet;  
  2.   
  3. import com.hemi.rhet.R;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.KeyEvent;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.CheckBox;  
  13. import android.widget.EditText;  
  14.   
  15. public class LoginWindow extends Activity {  
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         System.out.println("enter LoginWindow.onCreate()!");  
  20.           
  21.         super.onCreate(savedInstanceState);  
  22.           
  23.         setContentView(R.layout.login_window);  
  24.           
  25.         mUserName = (EditText)findViewById(R.id.username);  
  26.         mUserPasswd = (EditText)findViewById(R.id.userpasswd);  
  27.           
  28.         cbx_cmwap = (CheckBox) findViewById(R.id.cbx_cmwap);  
  29.   
  30.         loginButton = (Button) findViewById(R.id.login_button);  
  31.         exitButton = (Button) findViewById(R.id.exit_button);  
  32.           
  33.         loginBtnListener = new View.OnClickListener() {  
  34.             public void onClick(View view) {  
  35.                 LoginWindow.isCmwap = cbx_cmwap.isChecked();  
  36.                   
  37.                 if (view == loginButton) {  
  38.                     launchFetion();  
  39.                 } else if(view == exitButton) {  
  40.                     finish();  
  41.                 }  
  42.             }  
  43.         };  
  44.   
  45.         loginButton.setOnClickListener(loginBtnListener);  
  46.         exitButton.setOnClickListener(loginBtnListener);  
  47.     }  
  48.       
  49.     private void launchFetion() {  
  50.         Intent i = new Intent(this, FuncSelector.class);  
  51.         i.putExtra(KEY_LOGIN_NAME, mUserName.getText().toString());  
  52.         i.putExtra(KEY_LOGIN_PASSWD, mUserPasswd.getText().toString());  
  53.         i.putExtra(KEY_LOGIN_TYPE, cbx_cmwap.isChecked());  
  54.           
  55.         startActivity(i);  
  56.     }  
  57.       
  58.     @Override  
  59.     public boolean onKeyDown(int keyCode, KeyEvent msg) {  
  60. //        System.out.println("enter onKeyDown() in LoginWindow!");  
  61. //          
  62. //        if (null != loginBtnListener) {  
  63. //            View aview = getCurrentFocus();  
  64. //            loginBtnListener.onClick(aview);  
  65. //        }  
  66.         return false;  
  67.     }  
  68.       
  69.     private Button loginButton, exitButton;  
  70.     private EditText mUserName;  
  71.     private EditText mUserPasswd;  
  72.     private CheckBox cbx_cmwap;  
  73.       
  74.     private OnClickListener loginBtnListener;  
  75.       
  76.     public static final String KEY_LOGIN_NAME = "login_name";  
  77.     public static final String KEY_LOGIN_PASSWD = "login_passwd";  
  78.     public static final String KEY_LOGIN_TYPE = "login_type";  
  79.   
  80.     public static boolean isCmwap = false;  
  81. }  

2. FuncSelector.java -- 功能模块选择窗口

  1. package com.hemi.rhet;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import com.hemi.rhet.R;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.Intent;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.Menu;  
  13. import android.view.MenuItem;  
  14. import android.view.View;  
  15. import android.widget.AdapterView;  
  16. import android.widget.GridView;  
  17. import android.widget.SimpleAdapter;  
  18. import android.widget.AdapterView.OnItemClickListener;  
  19.   
  20. public class FuncSelector extends Activity {  
  21.   
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         Log.i("info""enter LoginWindow.onCreate()!");  
  25.         super.onCreate(savedInstanceState);  
  26.   
  27.         setContentView(R.layout.func_selector);  
  28.   
  29.         initFuncGrids();  
  30.     }  
  31.   
  32.     private void initFuncGrids() {  
  33.         GridView funcSeleView = (GridView) findViewById(R.id.func_selector);  
  34.   
  35.         // 生成动态数组,并且转入数据  
  36.         ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();  
  37.   
  38.         HashMap<String, Object> map = new HashMap<String, Object>();  
  39.         map.put("ItemImage", R.drawable.photo_upload);          // 添加图像资源的ID  
  40.         map.put("ItemText", getString(R.string.photo_upload));  // 按序号做ItemText  
  41.         lstImageItem.add(map);  
  42.           
  43.         map = new HashMap<String, Object>();  
  44.         map.put("ItemImage", R.drawable.icon);  
  45.         map.put("ItemText", getString(R.string.sys_config));  
  46.         lstImageItem.add(map);  
  47.           
  48.         for (int i = 1; i <= 10; i++) {  
  49.             map = new HashMap<String, Object>();  
  50.             map.put("ItemImage", R.drawable.icon);          // 添加图像资源的ID  
  51.             map.put("ItemText""NO." + String.valueOf(i)); // 按序号做ItemText  
  52.             lstImageItem.add(map);  
  53.         }  
  54.               
  55.         // 生成适配器的ImageItem <====> 动态数组的元素,两者一一对应  
  56.         SimpleAdapter saImageItems = new SimpleAdapter(this// 没什么解释  
  57.                 lstImageItem,// 数据来源  
  58.                 R.layout.night_item,// night_item的XML实现  
  59.   
  60.                 // 动态数组与ImageItem对应的子项  
  61.                 new String[] { "ItemImage""ItemText" },  
  62.   
  63.                 // ImageItem的XML文件里面的一个ImageView,两个TextView ID  
  64.                 new int[] {R.id.ItemImage,R.id.ItemText});  
  65.                 //null);  
  66.           
  67.         // 添加并且显示  
  68.         funcSeleView.setAdapter(saImageItems);  
  69.         //saImageItems.notifyDataSetChanged();  
  70.           
  71.         // 添加消息处理  
  72.         funcSeleView.setOnItemClickListener(new ItemClickListener());  
  73.     }  
  74.       
  75.     public boolean onCreateOptionsMenu(Menu menu) {  
  76.         super.onCreateOptionsMenu(menu);  
  77.         menu.add(0, EXIT_ID,0, R.string.back_button);  
  78.           
  79.         return true;  
  80.     }  
  81.       
  82.     //@Override  
  83.     public boolean onMenuItemSelected(int featureId, MenuItem item) {  
  84.         boolean result = true;  
  85.           
  86.         switch(item.getItemId()) {  
  87.         case EXIT_ID:  
  88.             this.finish();  
  89.             break;  
  90.               
  91.         default:  
  92.             result = super.onMenuItemSelected(featureId, item);  
  93.             break;  
  94.         }  
  95.           
  96.         return result;   
  97.     }  
  98.   
  99.     // 当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件  
  100.     class ItemClickListener implements OnItemClickListener {  
  101.         public void onItemClick(AdapterView<?> arg0,// The AdapterView where the  
  102.                 // click happened  
  103.                 View arg1,// The view within the AdapterView that was clicked  
  104.                 int arg2,// The position of the view in the adapter  
  105.                 long arg3// The row id of the item that was clicked  
  106.         ) {  
  107.             // 在本例中arg2=arg3  
  108.             HashMap<String, Object> item = (HashMap<String, Object>) arg0  
  109.                     .getItemAtPosition(arg2);  
  110.   
  111.             String tmpStr = (String) item.get("ItemText");  
  112.             //item.put("ItemText", tmpStr + tmpStr.substring(tmpStr.length() - 1));  
  113.   
  114.             // 显示所选Item的ItemText  
  115.             // setTitle((String)item.get("ItemText"));  
  116.             Log.i("info", (String) item.get("ItemText"));  
  117.   
  118.             ((SimpleAdapter) arg0.getAdapter()).notifyDataSetChanged();  
  119.               
  120.             Intent i;  
  121.             switch (arg2) {  
  122.             case 0:  
  123.                 i = new Intent();  
  124.                 i.setClass(FuncSelector.this, PhotoUpload.class);  
  125.                 startActivity(i);  
  126.                 break;  
  127.                   
  128.             case 1:  
  129.                 i = new Intent();  
  130.                 i.setClass(FuncSelector.this, ConfigWindow.class);  
  131.                 startActivity(i);  
  132.                 break;  
  133.                   
  134.             default:  
  135.                 break;  
  136.             }  
  137.         }  
  138.   
  139.     }  
  140.   
  141.     private static final int TAKE_PHOTO_ID = Menu.FIRST;  
  142.     private static final int UPLOAD_PHOTO_ID = Menu.FIRST + 1;  
  143.     private static final int EXIT_ID = Menu.FIRST + 3;  
  144.       
  145. }//FuncSelector  
  

3. PhotoUpload.java -- 照片上传模块

  1. package com.hemi.rhet;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.DataOutputStream;  
  6. import java.io.File;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileNotFoundException;  
  9. import java.io.FileOutputStream;  
  10. import java.io.IOException;  
  11. import java.io.InputStreamReader;  
  12. import java.io.OutputStream;  
  13. import java.io.UnsupportedEncodingException;  
  14. import java.net.HttpURLConnection;  
  15. import java.net.MalformedURLException;  
  16. import java.net.URL;  
  17. import java.text.SimpleDateFormat;  
  18. import java.util.ArrayList;  
  19. import java.util.Date;  
  20. import java.util.HashMap;  
  21. import java.util.List;  
  22.   
  23. import org.apache.http.Header;  
  24. import org.apache.http.HttpResponse;  
  25. import org.apache.http.NameValuePair;  
  26. import org.apache.http.client.ClientProtocolException;  
  27. import org.apache.http.client.HttpClient;  
  28. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  29. import org.apache.http.client.methods.HttpPost;  
  30. import org.apache.http.entity.FileEntity;  
  31. import org.apache.http.impl.client.DefaultHttpClient;  
  32. import org.apache.http.message.BasicNameValuePair;  
  33.   
  34. import com.hemi.rhet.R;  
  35.   
  36. import android.app.Activity;  
  37. import android.content.Intent;  
  38. import android.graphics.Bitmap;  
  39. import android.net.Uri;  
  40. import android.os.Bundle;  
  41. import android.os.Environment;  
  42. import android.util.Log;  
  43. import android.view.Menu;  
  44. import android.view.MenuItem;  
  45. import android.view.View;  
  46. import android.widget.AdapterView;  
  47. import android.widget.GridView;  
  48. import android.widget.ImageView;  
  49. import android.widget.SimpleAdapter;  
  50. import android.widget.Toast;  
  51. import android.widget.AdapterView.OnItemClickListener;  
  52.   
  53. public class PhotoUpload extends Activity {  
  54.   
  55.     @Override  
  56.     public void onCreate(Bundle savedInstanceState) {  
  57.         Log.i("info""enter LoginWindow.onCreate()!");  
  58.         super.onCreate(savedInstanceState);  
  59.   
  60.         setContentView(R.layout.func_selector);  
  61.     }  
  62.   
  63.     @Override  
  64.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  65.         super.onActivityResult(requestCode, resultCode, data);  
  66.           
  67.         if (TAKE_PHOTO_ID == requestCode) {  
  68.             if (resultCode != RESULT_OK) return;  
  69.             Bundle extras = data.getExtras();  
  70.             try {  
  71.                 Bitmap photoCaptured = (Bitmap) extras.get("data");  
  72.                 ImageView img = new ImageView(this);  
  73.                 img.setImageBitmap(photoCaptured);  
  74.                 setContentView(img);  
  75.   
  76.                 //store to sd card  
  77.                 ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  78.                 photoCaptured.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  79.                 byte[] photoBytes = baos.toByteArray();  
  80.   
  81.                 File aFile = new File(getDatedFName(SD_CARD_TEMP_DIR));  
  82.                 photoPath = aFile.getAbsolutePath();  
  83.                   
  84.                 boolean b;  
  85.                 if (aFile.exists()) b = aFile.delete();  
  86.                 //f.mkdirs();  
  87.                 aFile.createNewFile();  //need add permission to manifest  
  88.                   
  89.                 FileOutputStream fos = new FileOutputStream(aFile);  
  90.                 fos.write(photoBytes);  
  91.                 fos.close();  
  92.   
  93.                 Log.d("info""onPictureTaken - wrote bytes: "  
  94.                         + photoBytes.length);  
  95.   
  96.                 Uri capturedImage = Uri  
  97.                         .parse(android.provider.MediaStore.Images.Media  
  98.                                 .insertImage(getContentResolver(), aFile  
  99.                                         .getAbsolutePath(), nullnull));  
  100.                 Log.i("camera""Selected image: " + capturedImage.toString());  
  101.   
  102.             } catch (FileNotFoundException e) {  
  103.                 e.printStackTrace();  
  104.             } catch (IOException e) {  
  105.                 e.printStackTrace();  
  106.             }               
  107.   
  108.         } else if (UPLOAD_PHOTO_ID == requestCode) {  
  109.               
  110.         }  
  111.     }   
  112.       
  113.     public boolean onCreateOptionsMenu(Menu menu) {  
  114.         super.onCreateOptionsMenu(menu);  
  115.         menu.add(0, TAKE_PHOTO_ID,0, R.string.take_photo);  
  116.         menu.add(0, UPLOAD_PHOTO_ID,0, R.string.upload_photo);  
  117.         menu.add(0, BACK_ID,0, R.string.back_button);  
  118.           
  119.         return true;  
  120.     }  
  121.       
  122.     //@Override  
  123.     public boolean onMenuItemSelected(int featureId, MenuItem item) {  
  124.         boolean result = true;  
  125.           
  126.         switch(item.getItemId()) {  
  127.         case TAKE_PHOTO_ID:  
  128.             Log.i("info""ready to take photos!");  
  129.   
  130.             Intent i = new Intent("android.media.action.IMAGE_CAPTURE");  
  131.             startActivityForResult(i, TAKE_PHOTO_ID);  
  132.             result = true;  
  133.             break;  
  134.   
  135.         case UPLOAD_PHOTO_ID:  
  136.             uploadFile2Svr();  
  137.             break;  
  138.               
  139.         case BACK_ID:  
  140.             this.finish();  
  141.             break;  
  142.               
  143.         default:  
  144.             result = super.onMenuItemSelected(featureId, item);  
  145.             break;  
  146.         }  
  147.           
  148.         return result;   
  149.     }  
  150.       
  151.     public void uploadFile2Svr() {  
  152.         HttpClient httpclient = new DefaultHttpClient();  
  153.         String urlStr = new StringBuffer().append(HTTP_PROTOCOL)  
  154.             .append(/*SERVER_IP*/ConfigWindow.getServerIp())  
  155.             .append(':')  
  156.             .append(/*SERVER_PORT*/ConfigWindow.getServerPort())  
  157.             .append(FILE_UPLOADER_URL)  
  158.             .toString();  
  159.         HttpPost httppost = new HttpPost(urlStr);  
  160.           
  161.         String uploadMsg = "上传 照片失败!";  
  162.         try {  
  163.             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
  164.             // Your DATA  
  165.             nameValuePairs.add(new BasicNameValuePair("filename", ("IMAGE.jpg")) );  
  166. //      nameValuePairs.add(new BasicNameValuePair("orderno", "1"));  
  167. //      nameValuePairs.add(new BasicNameValuePair("userid", "123"));  
  168. //      nameValuePairs.add(new BasicNameValuePair("attach_type", "1"));  
  169. //      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
  170.               
  171.             File aFile = new File(photoPath);  
  172.             Log.i("info -- photoPath: ", photoPath);  
  173.             FileEntity fileEty = new FileEntity(aFile, "binary/octet-stream");  
  174.             httppost.setEntity(fileEty);  
  175.             httppost.addHeader("filename"/*("IMAGE.jpg")*/aFile.getName());  
  176.               
  177.             HttpResponse response;  
  178.             response = httpclient.execute(httppost);  
  179.             //Log.i("info -- response: ", response.getStatusLine().getReasonPhrase());  
  180.               
  181.             Header[] headers = response.getAllHeaders();  
  182.             headers = response.getHeaders("resultcode");  
  183.             if (headers[0].getValue().equals("0")) {  
  184.                 uploadMsg = "上传照片成功!";  
  185.             }  
  186.               
  187.         } catch (UnsupportedEncodingException e) {  
  188.             //e.printStackTrace();  
  189.             uploadMsg += e.toString();  
  190.             Log.e("exception", e.toString());  
  191.         } catch (ClientProtocolException e) {  
  192.             //e.printStackTrace();  
  193.             uploadMsg += e.toString();  
  194.             Log.e("exception", e.toString());  
  195.         } catch (IOException e) {  
  196.             //e.printStackTrace();  
  197.             uploadMsg += e.toString();  
  198.             Log.e("exception", e.toString());  
  199.         } finally {  
  200.             Toast.makeText(PhotoUpload.this, uploadMsg,  
  201.                     Toast.LENGTH_LONG).show();  
  202.             httpclient.getConnectionManager().shutdown();  
  203.         }  
  204.     }  
  205.       
  206.     public void uploadFile2Svr2() {  
  207.         BufferedReader in = null;     
  208.   
  209.         HttpClient httpclient = new DefaultHttpClient();  
  210.         String urlStr = new StringBuffer().append(HTTP_PROTOCOL)  
  211.           .append(ConfigWindow.getServerIp())  
  212.           .append(ConfigWindow.getServerPort())  
  213.             .append(FILE_UPLOADER_URL)  
  214.             .toString();  
  215.           
  216.         URL url = null;  
  217.         try {  
  218.             url = new URL(urlStr);  
  219.         } catch (MalformedURLException e1) {  
  220.             e1.printStackTrace();  
  221.         }  
  222.           
  223.         HttpURLConnection conn = null;  
  224.         DataOutputStream dos = null;  
  225.           
  226.         String lineEnd = "\r\n";  
  227.         String twoHyphens = "--";  
  228.         String boundary = "*****";   
  229.         int maxBufferSize = 16 * 1024;  
  230.   
  231.         try {  
  232. //          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
  233. //          // Your DATA  
  234. //          nameValuePairs.add(new BasicNameValuePair("filename", getDatedFName("IMAGE.jpg")) );  
  235. //          nameValuePairs.add(new BasicNameValuePair("orderno", "1"));  
  236. //          nameValuePairs.add(new BasicNameValuePair("userid", "123"));  
  237. //          nameValuePairs.add(new BasicNameValuePair("attach_type", "1"));  
  238.             //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
  239.   
  240.   
  241.             // Open a HTTP connection to the URL  
  242.             conn = (HttpURLConnection) url.openConnection();  
  243.               
  244.             conn.setConnectTimeout(120000);  
  245.   
  246.             // Allow Inputs  
  247.             conn.setDoInput(true);  
  248.             // Allow Outputs  
  249.             conn.setDoOutput(true);  
  250.             // Don't use a cached copy.  
  251.             conn.setUseCaches(false);  
  252.   
  253.             // Use a post method.  
  254.             conn.setRequestMethod("POST");  
  255.   
  256.             conn.setRequestProperty("Connection""Keep-Alive");  
  257.   
  258.             conn.setRequestProperty("Content-Type",  
  259.                     //"multipart/form-data;boundary=" + boundary);  
  260.                 "application/x-www-form-urlencoded");   
  261.               
  262.              conn.setRequestProperty("user-agent",  
  263.                      "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 GTB6");  
  264.              //conn.setRequestProperty("accept-language", "zh-cn,zh;q=0.5");  
  265.              //conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);  
  266.               
  267.              conn.connect();  
  268.   
  269.              //OutputStream connOs = conn.getOutputStream();  
  270.              dos = new DataOutputStream(conn.getOutputStream());  
  271.   
  272.             dos.writeBytes(twoHyphens + boundary + lineEnd);  
  273.             dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""  
  274.                             + "exsistingFileName" + "\"" + lineEnd);  
  275.             //dos.writeBytes(lineEnd);  
  276.   
  277.             Log.i("info""Headers are written");  
  278.   
  279.             // upload file to webserver via http  
  280.             FileInputStream fileInputStream = new FileInputStream(photoPath);  
  281.             // create a buffer of maximum size  
  282.             int bytesAvailable = fileInputStream.available();  
  283.             int bufferSize = Math.min(bytesAvailable, maxBufferSize);  
  284.             byte[] buffer = new byte[bufferSize];  
  285.   
  286.             // read file and write it into form...  
  287.   
  288.             int bytesRead = fileInputStream.read(buffer, 0, bufferSize);  
  289.   
  290.             while (bytesRead > 0) {  
  291.                 dos.write(buffer, 0, bufferSize);  
  292.                 bytesAvailable = fileInputStream.available();  
  293.                 bufferSize = Math.min(bytesAvailable, maxBufferSize);  
  294.                 bytesRead = fileInputStream.read(buffer, 0, bufferSize);  
  295.             }  
  296.   
  297.             // send multipart form data necesssary after file data...  
  298.             dos.writeBytes(lineEnd);  
  299.             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);  
  300.   
  301.             // close streams  
  302.             Log.e("info""File is written");  
  303.             fileInputStream.close();  
  304.   
  305.             dos.flush();  
  306.             dos.close();  
  307.             dos = null;  
  308.   
  309.             // response  
  310.             // HttpResponse response;  
  311.             // response = httpclient.execute(httppost);  
  312.             // response = httpclient.execute(conn.get);  
  313.   
  314.             in = new BufferedReader(  
  315.                     new InputStreamReader(conn.getInputStream()));  
  316.             StringBuffer sb = new StringBuffer("");  
  317.             String line = "";  
  318.             String NL = System.getProperty("line.separator");  
  319.             while ((line = in.readLine()) != null) {  
  320.                 sb.append(line + NL);  
  321.             }  
  322.             in.close();  
  323.             String result = sb.toString();  
  324.             Log.i("info", result);     
  325.   
  326.         } catch (ClientProtocolException e) {  
  327.             e.printStackTrace();  
  328.         } catch (IOException e) {  
  329.             e.printStackTrace();  
  330.         } finally{     
  331.             if(in != null){     
  332.                 try{     
  333.                     in.close();     
  334.                 }catch(IOException ioe){     
  335.                     Log.e("error", ioe.toString());     
  336.                 }     
  337.             }    
  338.         }  
  339.     }  
  340.       
  341.     public static String getDatedFName(String fname) {  
  342.         StringBuffer result = new StringBuffer();  
  343.   
  344.         SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");  
  345.         String dateSfx = "_" + df.format(new Date());  
  346.   
  347.         int idx = fname.lastIndexOf('.');  
  348.         if (idx != -1) {  
  349.             result.append(fname.substring(0, idx));  
  350.             result.append(dateSfx);  
  351.             result.append(fname.substring(idx));  
  352.         } else {  
  353.             result.append(fname);  
  354.             result.append(dateSfx);  
  355.         }  
  356.   
  357.         return result.toString();  
  358.     }  
  359.       
  360.     //=============================================  
  361.     //private Bitmap photoCaptured;  
  362.     private String photoPath = "/sdcard/IMAGE_100225083437.jpg";  //"/sdcard/1.txt";  
  363.       
  364.       private static final int TAKE_PHOTO_ID = Menu.FIRST;  
  365.     private static final int UPLOAD_PHOTO_ID = Menu.FIRST + 1;  
  366.     private static final int BACK_ID = Menu.FIRST + 3;  
  367.       
  368.     private static final String HTTP_PROTOCOL = "http://";  
  369.     private static final String FILE_UPLOADER_URL = "/fileuploader/system/fileUpload";  
  370.       
  371.     private String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "IMG.jpg";   
  372.   
  373. }  
  

4. ConfigWindow.java--系统配置窗口

  1. package com.hemi.rhet;  
  2.   
  3. import com.hemi.rhet.R;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.KeyEvent;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.CheckBox;  
  14. import android.widget.EditText;  
  15.   
  16. public class ConfigWindow extends Activity {  
  17.   
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         System.out.println("enter ConfigWindow.onCreate()!");  
  21.           
  22.         super.onCreate(savedInstanceState);  
  23.           
  24.         setContentView(R.layout.config_window);  
  25.           
  26.         mServerIP = (EditText)findViewById(R.id.serverip);  
  27.         mServerPort = (EditText)findViewById(R.id.serverport);  
  28.         //hemerr  
  29.         mServerIP.setText(serverIp);  
  30.         mServerPort.setText(serverPort);  
  31.           
  32.         okButton = (Button) findViewById(R.id.ok_button);  
  33.         backButton = (Button) findViewById(R.id.back_button);  
  34.           
  35.         loginBtnListener = new View.OnClickListener() {  
  36.             public void onClick(View view) {  
  37.                 if (view == okButton) {  
  38.                     serverIp = mServerIP.getText().toString();  
  39.                     serverPort = mServerPort.getText().toString();  
  40.                     Log.i("info""IP is: "+serverIp+"\tPort is: "+serverPort);  
  41.                       
  42.                     finish();  
  43.                       
  44.                 } else if(view == backButton) {  
  45.                     finish();  
  46.                 }  
  47.             }  
  48.         };  
  49.   
  50.         okButton.setOnClickListener(loginBtnListener);  
  51.         backButton.setOnClickListener(loginBtnListener);  
  52.     }  
  53.       
  54.     private void launchFetion() {  
  55.         Intent i = new Intent(this, FuncSelector.class);  
  56.         i.putExtra(KEY_LOGIN_NAME, mServerIP.getText().toString());  
  57.         i.putExtra(KEY_LOGIN_PASSWD, mServerPort.getText().toString());  
  58.           
  59.         startActivity(i);  
  60.     }  
  61.       
  62.     @Override  
  63.     public boolean onKeyDown(int keyCode, KeyEvent msg) {  
  64. //        System.out.println("enter onKeyDown() in LoginWindow!");  
  65. //          
  66. //        if (null != loginBtnListener) {  
  67. //            View aview = getCurrentFocus();  
  68. //            loginBtnListener.onClick(aview);  
  69. //        }  
  70.         return false;  
  71.     }  
  72.       
  73.     public static String getServerIp() {  
  74.         return serverIp;  
  75.     }  
  76.   
  77.     public static String getServerPort() {  
  78.         return serverPort;  
  79.     }  
  80.       
  81.     private Button okButton, backButton;  
  82.     private EditText mServerIP;  
  83.     private EditText mServerPort;  
  84.       
  85.     private OnClickListener loginBtnListener;  
  86.       
  87.     public static final String KEY_LOGIN_NAME = "login_name";  
  88.     public static final String KEY_LOGIN_PASSWD = "login_passwd";  
  89.     public static final String KEY_LOGIN_TYPE = "login_type";  
  90.   
  91.     public static String serverIp = "192.168.0.98";  //;  
  92.     public static String serverPort = "8081";  
  93. }  
  

还需要增加bg_logo.jpg、icon.png、photo_upload.png等几个图片。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多