編輯:中級開發
代碼部分(SubmitPhoto.Java):
vIEw plaincopy to clipboardprint?
01.package com.bjwmt.camera;
02.import Java.io.BufferedReader;
03.import Java.io.File;
04.import Java.io.FileInputStream;
05.import Java.io.IOException;
06.import Java.io.InputStreamReader;
07.import Java.Net.MalformedURLException;
08.import Java.Net.URL;
09.import Java.Net.URLConnection;
10.import Java.util.ArrayList;
11.import Java.util.List;
12.import org.apache.commons.Net.ftp.FTP;
13.import org.apache.commons.Net.ftp.FTPClIEnt;
14.import org.apache.commons.Net.ftp.FTPReply;
15.import android.app.Activity;
16.import android.app.AlertDialog;
17.import android.app.Dialog;
18.import android.app.ProgressDialog;
19.import android.content.ComponentName;
20.import android.content.Context;
21.import android.content.DialogInterface;
22.import android.content.Intent;
23.import android.content.pm.ActivityInfo;
24.import android.database.Cursor;
25.import android.graphics.Bitmap;
26.import android.graphics.BitmapFactory;
27.import android.graphics.drawable.BitmapDrawable;
28.import android.Net.ConnectivityManager;
29.import android.net.NetworkInfo;
30.import android.os.Bundle;
31.import android.os.Handler;
32.import android.os.Message;
33.import android.telephony.TelephonyManager;
34.import android.text.Html;
35.import android.util.Log;
36.import android.view.VIEw;
37.import android.vIEw.WindowManager;
38.import android.view.VIEw.OnClickListener;
39.import android.widget.Button;
40.import android.widget.EditText;
41.import android.widget.ImageVIEw;
42.import android.widget.Spinner;
43.public class SubmitPhoto extends Activity{
44.private Button subButton;
45.private Button returnButton;
46.private ImageView subPicImgVIEw;
47.private Bundle preBundle;
48.private MyThread myThread;
49.// private Spinner stationSpinner;
50.private DBHelper helper;
51.private Cursor cursor;
52.private List<StationModel> list = new ArrayList<StationModel>();
53.// private MyThread thread = new MyThread();
54.@Override
55. public void onCreate(Bundle savedInstanceState){
56.super.onCreate(savedInstanceState);
57.setContentVIEw(R.layout.photo_submit);
58.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //橫屏
59.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏
60.setListener();
61.Bitmap bitmap = getBitMap();
62.BitmapDrawable bmpDraw=new BitmapDrawable(bitmap);
63.subPicImgVIEw.setImageDrawable(bmpDraw);
64.bitmap = null;
65.}
66./**
67.* 給界面上的按鈕及控件添加Listener
68.*/
69.public void setListener(){
70.preBundle = this.getIntent().getExtras();
71.subPicImgView = (ImageView)findViewById(R.id.subPicImgVIEw);
72.subButton = (Button)findVIEwById(R.id.subButton);
73.returnButton = (Button)findVIEwById(R.id.returnMain);
74.// stationSpinner = (Spinner)findVIEwById(R.id.stationId);
75.subButton.setOnClickListener(subClick);
76.returnButton.setOnClickListener(returnClick);
77.helper = new DBHelper(this);
78.helper.open(this);
79.cursor = helper.loadAll();
80.cursor.moveToFirst();
81.if(!cursor.isAfterLast()){
82.int modelId = cursor.getColumnIndex("ID");
83.int stName = cursor.getColumnIndex("STNAME");
84.int stid = cursor.getColumnIndex("STID");
85.int addvcd = cursor.getColumnIndex("FATHER");
86.StationModel sm = new StationModel();
87.sm.setId(cursor.getInt(modelId));
88.sm.setStid(cursor.getInt(stid));
89.sm.setStname(cursor.getString(stName));
90.sm.setAddvcd(cursor.getString(addvcd));
91.list.add(sm);
92.cursor.moveToNext();
93.}
94.cursor.close();
95.helper.close();
96.}
97./**
98.* 點擊上傳按鈕觸發
99.*/
100.private OnClickListener subClick = new OnClickListener(){
101.public void onClick(VIEw v) {
102.ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
103. NetworkInfo networkinfo = manager.getActiveNetworkInfo();
104. if (networkinfo == null || !networkinfo.isAvailable()) { // 當前網絡不可用
105. new AlertDialog.Builder(SubmitPhoto.this)
106. .setMessage("檢查到沒有可用的網絡連接,請打開網絡連接")
107. .setPositiveButton("確定",
108. new DialogInterface.OnClickListener(){
109. public void onClick(DialogInterface dialoginterface, int i){
110. ComponentName cn = new ComponentName("com.android.settings","com.android.settings.Settings");
111.Intent intent = new Intent();
112.intent.setComponent(cn);
113.intent.setAction("android.intent.action.VIEW");
114.startActivity(intent);
115.// finish();
116. }
117. }
118. ).show();
119. }else{
120. showDialog(0);
121. myThread = new MyThread();
122. myThread.start();
123. }
124.}
125.};
126./**
127.* 點擊返回按鈕時觸發的事件
128.*/
129.private OnClickListener returnClick = new OnClickListener(){
130.public void onClick(VIEw v) {
131.new AlertDialog.Builder(SubmitPhoto.this)
132..setTitle("提示").setMessage("確定放棄上傳?")
133..setPositiveButton("確定",
134.new DialogInterface.OnClickListener() {
135.public void onClick(DialogInterface dialog,
136.int whichButton) {
137.// if(!myThread.isInterrupted()){
138.// myThread.interrupt();
139.// }
140.Intent intent = new Intent();
141.intent.setClass(SubmitPhoto.this, MainPage.class);
142.finish();
143.startActivity(intent);
144.}
145.}).setNegativeButton("取消",
146.new DialogInterface.OnClickListener() {
147.public void onClick(DialogInterface dialog,
148.int whichButton) {
149.// 取消按鈕事件
150.}
151.}
152.).show();
153.}
154.};
155./**
156.* 將上個界面傳過來的圖片地址轉換成Bitmap返回
157.* @return
158.*/
159.public Bitmap getBitMap(){
160.String picPath = preBundle.getString("prePath");
161.BitmapFactory.Options options = new BitmapFactory.Options();
162. options.inSampleSize = 2;
163. Bitmap bm = BitmapFactory.decodeFile(picPath, options);
164.return bm;
165.}
166./**
167.* 當調用showDialog()時觸發此函數
168.* 控制彈出的內容
169.*/
170.@Override
171. protected Dialog onCreateDialog(int id) {
172. switch (id) {
173. case 0: {
174. ProgressDialog dialog = new ProgressDialog(SubmitPhoto.this);
175. dialog.setMessage("文件上傳中,請稍後...");
176. dialog.setIndeterminate(true);
177. dialog.setCancelable(true);
178.// Log.d("dialog","<<<<<<<<<<<<<<<<-----<<<<<<<<<dialoging");
179. return dialog;
180. }
181. case 1: {
182. ProgressDialog mProgressDialog = new ProgressDialog(SubmitPhoto.this);
183. mProgressDialog.setIcon(R.drawable.ic_menu_upload_mini);
184. mProgressDialog.setTitle("文件上傳中請稍後");
185. mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
186. mProgressDialog.setMax(100);
187. mProgressDialog.setButton("取消", new DialogInterface.OnClickListener() {
188. public void onClick(DialogInterface dialog, int whichButton) {
189.// removeDialog(1);
190. }
191. });
192. return mProgressDialog;
193. }
194. }
195. return null;
196. }
197./**
198.* 顯示Dialog的method
199.* @param mess
200.*/
201.private void showToast(String mess) {
202.// Log.d("showToast", "======================showToast");
203.new AlertDialog.Builder(SubmitPhoto.this).setTitle("Message")
204..setMessage(Html.fromHtml(mess)).setNegativeButton("確定",
205.new DialogInterface.OnClickListener() {
206.public void onClick(DialogInterface dialog, int which) {
207.// if(!myThread.isInterrupted()){
208.// myThread.interrupt();
209.// }
210.Intent intent = new Intent();
211. intent.setClass(SubmitPhoto.this, MainPage.class);
212. finish();
213. startActivity(intent);
214.}
215.}).show();
216.}
217./*
218.* 接收子線程傳遞過來的結果
219.* 負責給主線程發送消息
220.*/
221.Handler subHandler = new Handler() {
222. public void handleMessage(Message msg) {
223. switch (msg.what) {
224. case 0:
225. showDialog(0);
226. break;
227. case 1:
228. removeDialog(0);
229. Bundle bundle = msg.getData();
230.// Log.d("upload result",">>>>>>>>>>>>>>>>>>upload result:"+bundle.getString("uploadMsg"));
231. showToast(bundle.getString("uploadMsg"));
232. break;
233. case 2:
234. removeDialog(0);
235. showToast("您請求的網絡連接失敗,請稍後重試");
236. break;
237. case 3:
238. removeDialog(0);
239. showToast("<p>對不起,您的手機卡沒有被許可使用本程序進行上傳功能,如有問題請<a href="mailto:[email protected]" mce_href="mailto:c.com">聯系我們</a></p>");
240. break;
241. }
242. super.handleMessage(msg);
243. }
244.};
245./**
246.* 圖片上傳線程
247.* 將上傳結果通過Handler傳遞給主線程
248.* @author peter
249.*/
250.class MyThread extends Thread implements Runnable {
251.private String upMsg = "",file="",fileName="";
252./**
253.* 判斷網絡是否可用
254.*/
255.public int getCon(String url) {
256.int con1 = 0;
257.URL objURL = null;
258.try {
259.objURL = new URL(url);
260.con1 = 1;
261.} catch (MalformedURLException e2) {
262.con1 = 0;
263.}
264.if (objURL != null) {
265.URLConnection conn = null;
266.try {
267.conn = objURL.openConnection();
268.conn.setConnectTimeout(1000);
269.conn.setReadTimeout(1000);
270.con1 = 1;
271.} catch (IOException e) {
272.con1 = 0;
273.}
274.if (conn != null) {
275.try {
276.conn.connect();
277.con1 = 1;
278.} catch (IOException e) {
279.con1 = 0;
280.}
281.}
282.}
283.return con1;
284.}
285./**
286.* 取得請求返回的數據字符串
287.* @param url
288.* @return
289.*/
290.public String getText(String url){
291.String text = "";
292.try {
293.URL objURL = new URL(url);
294.URLConnection conn = objURL.openConnection();
295.conn.setConnectTimeout(6 * 1000);
296.conn.connect();
297.BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
298.String inputLine;
299.if (br == null) {
300.return null;
301.}
302.while ((inputLine = br.readLine()) != null) {
303.text += inputLine;
304.}
305.} catch (MalformedURLException e) {
306.e.printStackTrace();
307.} catch (IOException e) {
308.e.printStackTrace();
309.}
310.return text;
311.}
312./**
313.* 圖片上傳
314.*/
315.public void uploadFile(){
316.TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
317.FTPClIEnt ftp = new FTPClIEnt();
318.String server = "xx.xx.xx.xx";
319.String username = tm.getDeviceId();
320.String passWord = "micromulti";
321.try {
322.FTP.setDefaultTimeout(30000);
323.FTP.setDefaultPort(21);
324.FTP.connect(server);
325.int reply = FTP.getReplyCode();
326.if (!FTPReply.isPositiveCompletion(reply)) {
327.FTP.disconnect();
328.upMsg = "FTP 連接失敗";
329.} else {
330.if (FTP.login(username, passWord)) {
331.FTP.enterLocalPassiveMode();
332.ftp.setFileType(FTP.BINARY_FILE_TYPE);
333.FTP.setRemoteVerificationEnabled(false);
334.FileInputStream fis = new FileInputStream(new File(file));
335.FTP.storeFile(fileName, fis);
336.fis.close();
337. }
338. FTP.logout();
339. upMsg = "文件上傳成功";
340. }
341. } catch (Exception e) {
342. upMsg = ""+e;
343. } finally {
344. if (FTP.isConnected()) {
345. try {
346. FTP.disconnect();
347. } catch (Exception ioe) {
348. upMsg = ""+ioe;
349. }
350. }
351. }
352.}
353. public void run() {
354. String file = preBundle.getString("prePath");
355. String arrs[] = file.split("/");
356. String fileName = arrs[arrs.length-1];
357. String fileStrArr[] = fileName.split("-");
358. String phoneIMSI = fileStrArr[0];
359. String lat = fileStrArr[1];
360. String lng = fileStrArr[2];
361. String shootTime = fileStrArr[3];
362.// Log.d("lat&lng","*******************lat is:"+lat+" lng is:"+lng+" fileName is:"+fileName);
363. EditText picInfo = (EditText)findVIEwById(R.id.photoInfo);
364. String freshUrl ="http://xx.xx.xx.xx:8080/DataAcquisition/photoUploadAction.do?photoWebModel.actiontype=upload";
365. String postUrl = freshUrl+"&photoWebModel.phoneIMSI="+phoneIMSI
366. +"&photoWebModel.lat="+lat
367. +"&photoWebModel.lng="+lng
368. +"&photoWebModel.shootTime="+shootTime
369. +"&photoWebModel.picInfo="+picInfo.getText()
370. +"&photoWebModel.filename="+fileName;
371. Message message = new Message();
372. String result = "";
373. if(this.getCon(postUrl)==1){
374. result = getText(postUrl);
375. Log.d("uploadConfirmResult", ">>>>>>>>>>>>>>>>>>"+result.trim());
376. }else{
377. message.what = 2;
378. }
379. if(result.trim().equals("true")){
380. String thumbnailFilePath = "sdcard/fatalityUpload/Thumbnail_fatality/"+fileName;
381. uploadFile(); //執行圖片上傳
382. Log.d("uploadFile",">>>>>>>>>>>>>>>>>>>>>>>>>complate!stop Upload...");
383.File file2 = new File(file);
384.file2.delete();
385.file2 = null;
386.File file3 = new File(thumbnailFilePath);
387.file3.delete();
388.file3 = null;
389. Bundle bundle = new Bundle();
390. bundle.putString("uploadMsg", upMsg);
391. message.setData(bundle);
392. message.what = 1;
393.}else if (result.trim().equals("false1")){
394. message.what = 3;
395.}else{}
396. Log.d("mess.what", "<<<<<<<<<<<<<<<<<<message.what is :"+message.what);
397. subHandler.sendMessage(message);
398. }
399.}
400.}
package com.bjwmt.camera;
import Java.io.BufferedReader;
import Java.io.File;
import Java.io.FileInputStream;
import Java.io.IOException;
import Java.io.InputStreamReader;
import Java.Net.MalformedURLException;
import Java.Net.URL;
import Java.Net.URLConnection;
import Java.util.ArrayList;
import Java.util.List;
import org.apache.commons.Net.ftp.FTP;
import org.apache.commons.Net.ftp.FTPClIEnt;
import org.apache.commons.Net.ftp.FTPReply;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.Net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.Html;
import android.util.Log;
import android.view.VIEw;
import android.vIEw.WindowManager;
import android.view.VIEw.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageVIEw;
import android.widget.Spinner;
public class SubmitPhoto extends Activity{
private Button subButton;
private Button returnButton;
private ImageView subPicImgVIEw;
private Bundle preBundle;
private MyThread myThread;
// private Spinner stationSpinner;
private DBHelper helper;
private Cursor cursor;
private List<StationModel> list = new ArrayList<StationModel>();
// private MyThread thread = new MyThread();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.photo_submit);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //橫屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏
setListener();
Bitmap bitmap = getBitMap();
BitmapDrawable bmpDraw=new BitmapDrawable(bitmap);
subPicImgVIEw.setImageDrawable(bmpDraw);
bitmap = null;
}
/**
* 給界面上的按鈕及控件添加Listener
*/
public void setListener(){
preBundle = this.getIntent().getExtras();
subPicImgView = (ImageView)findViewById(R.id.subPicImgVIEw);
subButton = (Button)findVIEwById(R.id.subButton);
returnButton = (Button)findVIEwById(R.id.returnMain);
// stationSpinner = (Spinner)findVIEwById(R.id.stationId);
subButton.setOnClickListener(subClick);
returnButton.setOnClickListener(returnClick);
helper = new DBHelper(this);
helper.open(this);
cursor = helper.loadAll();
cursor.moveToFirst();
if(!cursor.isAfterLast()){
int modelId = cursor.getColumnIndex("ID");
int stName = cursor.getColumnIndex("STNAME");
int stid = cursor.getColumnIndex("STID");
int addvcd = cursor.getColumnIndex("FATHER");
StationModel sm = new StationModel();
sm.setId(cursor.getInt(modelId));
sm.setStid(cursor.getInt(stid));
sm.setStname(cursor.getString(stName));
sm.setAddvcd(cursor.getString(addvcd));
list.add(sm);
cursor.moveToNext();
}
cursor.close();
helper.close();
}
/**
* 點擊上傳按鈕觸發
*/
private OnClickListener subClick = new OnClickListener(){
public void onClick(VIEw v) {
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = manager.getActiveNetworkInfo();
if (networkinfo == null || !networkinfo.isAvailable()) { // 當前網絡不可用
new AlertDialog.Builder(SubmitPhoto.this)
.setMessage("檢查到沒有可用的網絡連接,請打開網絡連接")
.setPositiveButton("確定",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialoginterface, int i){
ComponentName cn = new ComponentName("com.android.settings","com.android.settings.Settings");
Intent intent = new Intent();
intent.setComponent(cn);
intent.setAction("android.intent.action.VIEW");
startActivity(intent);
// finish();
}
}
).show();
}else{
showDialog(0);
myThread = new MyThread();
myThread.start();
}
}
};
/**
* 點擊返回按鈕時觸發的事件
*/
private OnClickListener returnClick = new OnClickListener(){
public void onClick(VIEw v) {
new AlertDialog.Builder(SubmitPhoto.this)
.setTitle("提示").setMessage("確定放棄上傳?")
.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// if(!myThread.isInterrupted()){
// myThread.interrupt();
// }
Intent intent = new Intent();
intent.setClass(SubmitPhoto.this, MainPage.class);
finish();
startActivity(intent);
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 取消按鈕事件
}
}
).show();
}
};
/**
* 將上個界面傳過來的圖片地址轉換成Bitmap返回
* @return
*/
public Bitmap getBitMap(){
String picPath = preBundle.getString("prePath");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(picPath, options);
return bm;
}
/**
* 當調用showDialog()時觸發此函數
* 控制彈出的內容
*/
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0: {
ProgressDialog dialog = new ProgressDialog(SubmitPhoto.this);
dialog.setMessage("文件上傳中,請稍後...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
// Log.d("dialog","<<<<<<<<<<<<<<<<-----<<<<<<<<<dialoging");
return dialog;
}
case 1: {
ProgressDialog mProgressDialog = new ProgressDialog(SubmitPhoto.this);
mProgressDialog.setIcon(R.drawable.ic_menu_upload_mini);
mProgressDialog.setTitle("文件上傳中請稍後");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMax(100);
mProgressDialog.setButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// removeDialog(1);
}
});
return mProgressDialog;
}
}
return null;
}
/**
* 顯示Dialog的method
* @param mess
*/
private void showToast(String mess) {
// Log.d("showToast", "======================showToast");
new AlertDialog.Builder(SubmitPhoto.this).setTitle("Message")
.setMessage(Html.fromHtml(mess)).setNegativeButton("確定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// if(!myThread.isInterrupted()){
// myThread.interrupt();
// }
Intent intent = new Intent();
intent.setClass(SubmitPhoto.this, MainPage.class);
finish();
startActivity(intent);
}
}).show();
}
/*
* 接收子線程傳遞過來的結果
* 負責給主線程發送消息
*/
Handler subHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
showDialog(0);
break;
case 1:
removeDialog(0);
Bundle bundle = msg.getData();
// Log.d("upload result",">>>>>>>>>>>>>>>>>>upload result:"+bundle.getString("uploadMsg"));
showToast(bundle.getString("uploadMsg"));
break;
case 2:
removeDialog(0);
showToast("您請求的網絡連接失敗,請稍後重試");
break;
case 3:
removeDialog(0);
showToast("<p>對不起,您的手機卡沒有被許可使用本程序進行上傳功能,如有問題請<a href="mailto:[email protected]" mce_href="mailto:c.com">聯系我們</a></p>");
break;
}
super.handleMessage(msg);
}
};
簡介: 本文是共兩部分的系列文章 “android 和 iPhone 浏覽器之戰” 的第 2 部分,主要關注為 iPhone 和 Android 開發基於浏覽
在Android 3.0中除了我們重點講解的Fragment外,Action Bar也是一個重要的內容,Action Bar主要是用於代替傳統的標題欄,對於androi
簡介: 學習了解 IBM® Rational® Rhapsody® V7.5.2 版本中的新特性與改進之處,幫助系統管理員和實時、嵌入
簡介: BFS 是一款專門為 Linux 桌面環境所設計的內核調度器,它基於 Staircase Deadline 和 EEVDF 算法,支持 Linux 2