編輯:關於Android編程
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+1ve958Pm1vfSqtPQR3JpZFZpZXfX6bPJus2wtMWl1+mzyaOstbGwtM/C0ru49iYjMjY2ODQ7tePKsaOsu+G199PDz+C7+rvy1d/P4LLho6zFxNXVu/LV39Gh1PHP4LLh1dXGrKOs0aHU8c3qs8nWrrrzo6y9q8v1wtTNvM/Uyr7U2kdyaWRWaWV3o6zU2tXiwO/LtcP30rvPwqOsyOe5+0dyaWRWaWV3z9TKvrK7s/bAtKOsy7XD9828xqzMq7TzwcujrNDo0qrRucv1o6zU2s7StcTJz9K7xqqyqb/No6zP6s+4vbK94sHLzbzGrNG5y/W1xNStwO3T67n9s8yjrNXiwO+yu9TZ17jK9qGjPC9wPgo8cD7PwsPmzPnJz7T6wuujrDwvcD4KPHA+1ve958Pmo7o8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">package com.qian.pos;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Picture;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.qian.pos.util.BitmapUtil;
import com.qian.pos.util.FileUtils;
import com.qian.pos.util.PictureUtil;
import com.qian.pos.util.UploadUtil;
import com.qian.pos.util.UploadUtil.OnUploadProcessListener;
import com.qian.servletasynchttp.R;
public class ImageUploadActivity extends Activity// implements OnUploadProcessListener
{
private static final String TAG = "uploadImage";
protected static final int TO_UPLOAD_FILE = 1;
protected static final int UPLOAD_FILE_DONE = 2;
public static final int TO_SELECT_PHOTO = 3;
private static final int UPLOAD_INIT_PROCESS = 4;
private static final int UPLOAD_IN_PROCESS = 5;
private static String requestURL = "http://114.55.72.18/UnionPay/UploadAction";
private Button uploadButton;
//private ProgressBar progressBar;
private String picPath = null;
private ProgressDialog progressDialog;
private GridView list_gv;
private MyAdapter adapter;
private HashMap
選擇照片界面程序:
package com.qian.pos; import com.qian.servletasynchttp.R; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; public class SelectPicActivity extends Activity implements OnClickListener{ public static final int SELECT_PIC_BY_TACK_PHOTO = 1; public static final int SELECT_PIC_BY_PICK_PHOTO = 2; public static final String KEY_PHOTO_PATH = "photo_path"; private static final String TAG = "SelectPicActivity"; private LinearLayout dialogLayout; private Button takePhotoBtn,pickPhotoBtn,cancelBtn; /**獲取到的圖片路徑*/ private String picPath; private Intent lastIntent ; private Uri photoUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.select_pic_layout); initView(); } /** * 初始化加載View */ private void initView() { dialogLayout = (LinearLayout) findViewById(R.id.dialog_layout); dialogLayout.setOnClickListener(this); takePhotoBtn = (Button) findViewById(R.id.btn_take_photo); takePhotoBtn.setOnClickListener(this); pickPhotoBtn = (Button) findViewById(R.id.btn_pick_photo); pickPhotoBtn.setOnClickListener(this); cancelBtn = (Button) findViewById(R.id.btn_cancel); cancelBtn.setOnClickListener(this); lastIntent = getIntent(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.dialog_layout: finish(); break; case R.id.btn_take_photo: takePhoto(); break; case R.id.btn_pick_photo: pickPhoto(); break; default: finish(); break; } } /** * 拍照獲取圖片 */ private void takePhoto() { //執行拍照前,應該先判斷SD卡是否存在 String SDState = Environment.getExternalStorageState(); if(SDState.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE" /*** * 需要說明一下,以下操作使用照相機拍照,拍照後的圖片會存放在相冊中的 * 這裡使用的這種方式有一個好處就是獲取的圖片是拍照後的原圖 * 如果不實用ContentValues存放照片路徑的話,拍照後獲取的圖片為縮略圖不清晰 */ ContentValues values = new ContentValues(); photoUri = this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri); /**-----------------*/ startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO); }else{ Toast.makeText(this,"內存卡不存在", Toast.LENGTH_LONG).show(); } } /*** * 從相冊中取圖片 */ private void pickPhoto() { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO); } @Override public boolean onTouchEvent(MotionEvent event) { finish(); return super.onTouchEvent(event); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { doPhoto(requestCode,data); } super.onActivityResult(requestCode, resultCode, data); } /** * 選擇圖片後,獲取圖片的路徑 * @param requestCode * @param data */ private void doPhoto(int requestCode,Intent data) { if(requestCode == SELECT_PIC_BY_PICK_PHOTO ) //從相冊取圖片,有些手機有異常情況,請注意 { if(data == null) { Toast.makeText(this, "選擇圖片文件出錯", Toast.LENGTH_LONG).show(); return; } photoUri = data.getData(); if(photoUri == null ) { Toast.makeText(this, "選擇圖片文件出錯", Toast.LENGTH_LONG).show(); return; } } String[] pojo = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(photoUri, pojo, null, null,null); if(cursor != null ) { int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]); cursor.moveToFirst(); picPath = cursor.getString(columnIndex); cursor.close(); } Log.i(TAG, "imagePath = "+picPath); if (picPath != null /*&& (picPath.endsWith(".png") || picPath.endsWith(".PNG") || picPath.endsWith(".jpg") || picPath.endsWith(".JPG") || picPath.endsWith(".JPG") || picPath .endsWith(".JPEG"))*/) { lastIntent.putExtra(KEY_PHOTO_PATH, picPath); setResult(Activity.RESULT_OK, lastIntent); finish(); }else{ //Toast.makeText(this, picPath, Toast.LENGTH_LONG).show(); Toast.makeText(this, "選擇圖片文件不正確", Toast.LENGTH_LONG).show(); } } }
主要實現了圖片的上傳,上傳過程的初始化監聽和上傳完成的監聽,還有上傳耗時的計算,設置的回調接口監聽器在主界面可以設置,獲得必要的信息。
實現的批量的過程也在其中,使用HashMap將各個圖片的路徑保存在其中,然後HashMap的迭代器實現循環上傳,批量上傳的過程中,如果圖片過大會拋出異常,加載bitmap手機太多內存,沒來得及釋放又要加載另一張圖片,單張原圖上傳可以實現,但不符合市場需求,一般都是批量上傳。
程序的具體執行流程就不詳細說了,關鍵部分注釋已經寫好了。
package com.qian.pos.util; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.UUID; import android.content.Context; import android.util.Log; import android.widget.Toast; public class UploadUtil { private static UploadUtil uploadUtil; private static final String BOUNDARY = UUID.randomUUID().toString(); // 邊界標識 隨機生成 private static final String PREFIX = "--"; private static final String LINE_END = "\r\n"; private static final String CONTENT_TYPE = "multipart/form-data"; // 內容類型 private UploadUtil() { } /** * 單例模式獲取上傳工具類 * @return */ public static UploadUtil getInstance() { if (null == uploadUtil) { uploadUtil = new UploadUtil(); } return uploadUtil; } private static final String TAG = "UploadUtil"; private int readTimeOut = 10 * 10000; // 讀取超時 private int connectTimeout = 10 * 10000; // 超時時間 /*** * 請求使用多長時間 */ private static int requestTime = 0; private static final String CHARSET = "utf-8"; // 設置編碼 /*** * 上傳成功 */ public static final int UPLOAD_SUCCESS_CODE = 1; /** * 文件不存在 */ public static final int UPLOAD_FILE_NOT_EXISTS_CODE = 2; /** * 服務器出錯 */ public static final int UPLOAD_SERVER_ERROR_CODE = 3; protected static final int WHAT_TO_UPLOAD = 1; protected static final int WHAT_UPLOAD_DONE = 2; public boolean uploadFile(HashMapfilePathMap, String fileKey, String RequestURL, Map param) { String result = null; requestTime= 0; String fileName = ""; long requestTime = System.currentTimeMillis(); long responseTime = 0; try { // conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); Iterator iterator = filePathMap.keySet().iterator(); boolean isOk = true; System.out.println("filePathMap.size()"+filePathMap.size()); while(iterator.hasNext()) { URL url = new URL(RequestURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(readTimeOut); conn.setConnectTimeout(connectTimeout); conn.setDoInput(true); // 允許輸入流 conn.setDoOutput(true); // 允許輸出流 conn.setUseCaches(false); // 不允許使用緩存 conn.setRequestMethod("POST"); // 請求方式 conn.setRequestProperty("Charset", CHARSET); // 設置編碼 conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY); Integer next = iterator.next(); System.out.println(next+""); String filepath = filePathMap.get(next); switch (next.intValue()) { case 0: fileName = "IDPositive"; break; case 1: fileName = "IDNative"; break; case 2: fileName = "BusinessLicense"; break; case 3: fileName = "Outdoor"; break; case 4: fileName = "Indoor"; break; default: break; } DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); StringBuffer sb = null; String params = ""; if (param != null && param.size() > 0) { Iterator it = param.keySet().iterator(); while (it.hasNext()) { sb = null; sb = new StringBuffer(); String key = it.next(); String value = param.get(key); sb.append(PREFIX).append(BOUNDARY).append(LINE_END); sb.append("Content-Disposition: form-data; name=\"").append(key).append("\"").append(LINE_END).append(LINE_END); sb.append(value).append(LINE_END); params = sb.toString(); Log.i(TAG, key+"="+params+"##"); dos.write(params.getBytes()); // dos.flush(); } } sb = null; params = null; sb = new StringBuffer(); /** * 這裡重點注意: name裡面的值為服務器端需要key 只有這個key 才可以得到對應的文件 * filename是文件的名字,包含後綴名的 比如:abc.png */ sb.append(PREFIX).append(BOUNDARY).append(LINE_END); sb.append("Content-Disposition:form-data; name=\"" + fileKey + "\"; filename=\"" + fileName + "\"" + LINE_END); //+ "\"; filename=\"" + file.getName() + "\"" + LINE_END); sb.append("Content-Type:image/pjpeg" + LINE_END); // 這裡配置的Content-type很重要的 ,用於服務器端辨別文件的類型的 sb.append(LINE_END); params = sb.toString(); sb = null; //Log.i(TAG, filepath.getName()+"=" + params+"##"); //Log.i(TAG, ); dos.write(params.getBytes()); /**上傳文件*/ InputStream is = new FileInputStream(filepath); onUploadProcessListener.initUpload((int)filepath.length()); byte[] bytes = new byte[1024]; int len = 0; int curLen = 0; while ((len = is.read(bytes)) != -1) { curLen += len; dos.write(bytes, 0, len); onUploadProcessListener.onUploadProcess(curLen); } is.close(); is = null; dos.write(LINE_END.getBytes()); byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes(); dos.write(end_data); dos.flush(); dos.close(); dos = null; // dos.write(tempOutputStream.toByteArray()); /** * 獲取響應碼 200=成功 當響應成功,獲取響應的流 */ int res = conn.getResponseCode(); responseTime = System.currentTimeMillis(); this.requestTime = (int) ((responseTime-requestTime)/1000); Log.e(TAG, "response code:" + res); if (res == 200) { Log.e(TAG, "request success"); InputStream input = conn.getInputStream(); StringBuffer sb1 = new StringBuffer(); int ss; while ((ss = input.read()) != -1) { sb1.append((char) ss); } result = sb1.toString(); Log.e(TAG, "result : " + result); sb1 = null; if(fileName == "Indoor") { sendMessage(UPLOAD_SUCCESS_CODE, "上傳結果:"+ result); } conn.disconnect(); //return true; } else { Log.e(TAG, "request error"); sendMessage(UPLOAD_SERVER_ERROR_CODE,"上傳失敗:code=" + res); conn.disconnect(); isOk = false; } } return isOk; } catch (MalformedURLException e) { sendMessage(UPLOAD_SERVER_ERROR_CODE,"上傳失敗:error=" + e.getMessage()); e.printStackTrace(); return false; } catch (IOException e) { sendMessage(UPLOAD_SERVER_ERROR_CODE,"上傳失敗:error=" + e.getMessage()); e.printStackTrace(); return false; } } private void sendMessage(int responseCode,String responseMessage) { onUploadProcessListener.onUploadDone(responseCode, responseMessage); } /** * 下面是一個自定義的回調函數,用到回調上傳文件是否完成 * * @author shimingzheng * */ public static interface OnUploadProcessListener { void onUploadDone(int responseCode, String message); void onUploadProcess(int uploadSize); void initUpload(int fileSize); } private OnUploadProcessListener onUploadProcessListener; public void setOnUploadProcessListener( OnUploadProcessListener onUploadProcessListener) { this.onUploadProcessListener = onUploadProcessListener; } public int getReadTimeOut() { return readTimeOut; } public void setReadTimeOut(int readTimeOut) { this.readTimeOut = readTimeOut; } public int getConnectTimeout() { return connectTimeout; } public void setConnectTimeout(int connectTimeout) { this.connectTimeout = connectTimeout; } /** * 獲取上傳使用的時間 * @return */ public static int getRequestTime() { return requestTime; } public static interface uploadProcessListener{ } }
前面一篇文章講解了Android動畫Animator,但是不知道你有沒有發現,前面講解的所有的動畫都是針對某一Object來進行的,雖然我們可以對整個Layout添加動畫
Spinner控件是一個下拉列表 1.實現Spinner的系統默認設置 2.實現自定義設置: 3.使用的監聽器接口是:OnItemSelectedListener系統默認
自從寫了上篇《chrome 源碼研究啟航篇》後,到今天已經有了近一個月的時間,這段時間做了啥呢?研究到啥程度了呢?後續節奏是否有調整呢?針對上邊疑問,下面做逐個解答:這段
很多人在開發Android項目時沒有考慮過架構模式的問題,以至於隨著項目的增大,Activty或者Fragment中代碼也會越來越多,導致項目的維護變的越來越復雜。然而在