1,先寫一個佈局文件,拍照,選擇相片,listview顯示照片列表,刪除和上傳java
2,寫功能邏輯代碼android
private File ImageFile ; // 圖片的地址 private File VideoFile ; //視頻的地址 private String Image_GS = ".jpg" ; private String Video_GS = ".3gp" ; List<myphoto> list ; private Rcxc_Qz_Adapter adapter ; zxjc zxjc = new zxjc(); String filename [] ; File files [] ;文件數組 Map<String, Boolean> map ; // 用了封裝 選擇額結果,多張相片的時候須要記錄你選擇的相片。 //在baseactivity中須要判斷此文件到底有沒有,沒有的話必須建立 isfile() public void isFile(){ File f = new File(Environment.getExternalStorageDirectory()+"/Fpmsmsmobile/Image/"); if (!f.exists()) { f.mkdirs(); } File f1 = new File(Environment.getExternalStorageDirectory()+"/Fpmsmsmobile/Video/"); if (!f1.exists()) { f1.mkdirs(); } File f2 = new File(Environment.getExternalStorageDirectory()+"/Fpmsmsmobile/ImageThumbnil/"); if (!f2.exists()) { f2.mkdirs(); } File f3 = new File(Environment.getExternalStorageDirectory()+"/Fpmsmsmobile/ImageThumbnils/"); if (!f3.exists()) { f3.mkdirs(); } File f4 = new File(Environment.getExternalStorageDirectory()+"/Fpmsmsmobile/Signature/"); if (!f4.exists()) { f4.mkdirs(); } } map = new HashMap<String, Boolean>(); //獲取屏幕分辨率 DisplayMetrics dm = new DisplayMetrics(); //dm = getResources().getDisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); xdpi = dm.widthPixels; //點擊拍照的時候 qz_z.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (!CommonUtils.checkSdCard()) { toast(System.currentTimeMillis(), "請插入內存卡!"); return ; } Time = System.currentTimeMillis() ; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 0); // 獲取 sd 卡的位置 Environment.getExternalStorageDirectory() Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/Fpmsmsmobile/Image/image_"+Time+Image_GS)); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, 0); } }); //點擊選擇相片的時候 qz_s.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, 1); } }); //拍照或者選擇相片完成後須要執行的操做 @Override protected void onActivityResult(int arg0, int arg1, Intent arg2) { // TODO Auto-generated method stub super.onActivityResult(arg0, arg1, arg2); if (arg0 == 0) { // 獲取拍照返回的值 //這裏要保存相片到指定文件夾 ImageFile = new File(Environment.getExternalStorageDirectory(),"/Fpmsmsmobile/Image/image_"+Time+Image_GS); if (ImageFile.exists()){ mDialogshow(KsjcQzActivity.this,"正在保存", "保存中,請稍後·····"); //mDialogshow("正在上傳", "上傳中,請稍後·····"); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub BitmapUtils.saveBitmap(ImageFile.getPath(), bh,"rilt"); h.sendEmptyMessage(2); } }).start(); } }else if(arg0 == 1){ if (arg2 != null) { Uri uri = arg2.getData(); final String path = UriToPath(uri); //mDialogshow("正在上傳", "上傳中,請稍後·····"); mDialogshow(KsjcQzActivity.this,"正在保存", "保存中,請稍後·····"); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub BitmapUtils.saveBitmap(path, bh,"rilt"); h.sendEmptyMessage(2); } }).start(); } } //點擊上傳的時候 update.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (files != null) { mDialogshow(KsjcQzActivity.this,"正在保存", "保存中,請稍後·····"); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int j = 0; j < files.length; j++) { if (map.get(j+"")) { String baos = null ; try { //這裏將相片轉化爲流文件 而且文件加密 baos = BitmapUtils.encodeBase64File( files[j].getPath()); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()); //String result = rcxc.Uploadxcqzb(userid, userpassword, bsh, "照片", date, baos); //zfrybhid, password, guidbh, zbbh, filetype, filename, filedate, filecontent String result = "" ; //上傳相片 String guid = UUID.randomUUID().toString(); result = spyx.Add_ksjc_qkb_qz(zfrybh, mima, guid,bh,zfrybh, "照片", baos, date); boolean isTrue = false ; try { Integer.parseInt(result); isTrue = true ; } catch (Exception e) { // TODO: handle exception h.sendEmptyMessage(5); isTrue = false ; } if (isTrue) { files[j].delete(); } } } h.sendEmptyMessage(4); } }).start(); } } }); //顯示相片的listview還須要寫一個adapter class Rcxc_Qz_Adapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub if (files != null) { return files.length ; }else { return 0 ; } } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(final int position, View view, ViewGroup parent) { // TODO Auto-generated method stub view = LayoutInflater.from(KsjcQzActivity.this).inflate(R.layout.zxjc_qz_adapter, null); ImageView image = (ImageView) view.findViewById(R.id.rcjc_qz_adapter_image); TextView type = (TextView) view.findViewById(R.id.rcjc_qz_adapter_type); TextView guid = (TextView) view.findViewById(R.id.rcjc_qz_adapter_guid); final CheckBox check = (CheckBox) view.findViewById(R.id.rcjc_qz_adapter_check); /*hn_spsc_xcqzb qzb = list.get(position); if (qzb.filetype.equals("照片")) { BitmapFactory.Options opts = new Options(); opts.inSampleSize = 5 ; Bitmap bit = BitmapFactory.decodeByteArray(qzb.filecontent, 0, qzb.filecontent.length,opts) ; image.setImageBitmap(bit); }else { image.setImageResource(R.drawable.zygg); } type.setText("類型:"+qzb.filetype); guid.setText("時間:"+qzb.filedate);*/ BitmapFactory.Options opts = new Options(); opts.inSampleSize = 5 ; Bitmap bit = BitmapFactory.decodeFile(files[position].getPath(), opts) ; image.setImageBitmap(bit); type.setText("類型:"+"照片"); try { Date date = new Date(Long.parseLong(filename[position].split("_")[2])); String d ; if (xdpi > 799) { d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); }else { d = new SimpleDateFormat("yyyy-MM-dd").format(date); } guid.setText("時間:"+d); //guid.setText("時間:"+filename[position].split("_")[2]); } catch (Exception e) { // TODO: handle exception guid.setText("時間:"); } check.setChecked(map.get(position+"")); check.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //點擊選擇框記錄當前所選擇的值 map.put(position+"", check.isChecked()); } }); return view; } } //相片操做常常須要將uri轉化爲路徑 //將 URi 轉換成 絕對路徑 private String UriToPath(Uri uri){ String [] projection = { MediaStore.Images.Media.DATA } ; @SuppressWarnings("deprecation") Cursor cursor = managedQuery(uri, projection, null, null, null); int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(index); return path ; } // 將文件 加密 public static String encodeBase64File(String p) throws Exception { String path = bitmapPath(p); File file = new File(path); FileInputStream inputFile = new FileInputStream(file); byte[] buffer = new byte[(int)file.length()]; inputFile.read(buffer); inputFile.close(); //System.out.println(android.util.Base64.encodeToString(buffer,Base64.DEFAULT)); return android.util.Base64.encodeToString(buffer,Base64.DEFAULT); } //拍照完成或者選取相片之後生成縮略圖顯示在listview中 private void refresh(){ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub File file = new File(Environment.getExternalStorageDirectory()+"/Fpmsmsmobile/ImageThumbnil/"); filename = file.list() ; files = file.listFiles(); for (int i = 0; i < files.length; i++) { map.put(i+"", false); } if (files != null) { myLog("獲取數據 成功"); h.sendEmptyMessage(0); // 獲取數據 成功 }else { myLog("獲取數據 失敗"); h.sendEmptyMessage(1); // 獲取數據 失敗 } } }).start(); } 這裏還生成縮略圖在另一個文件中 public static void saveBitmap(String imagefile,String bsh,String riOrzx){ BitmapFactory.Options newOpts = new BitmapFactory.Options(); //開始讀入圖片,此時把options.inJustDecodeBounds 設回true了 不加載圖片 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(imagefile,newOpts);//此時返回bm爲空 newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; //如今主流手機比較可能是800*480分辨率,因此高和寬咱們設置爲 float hh = 800f;//這裏設置高度爲800f float ww = 480f;//這裏設置寬度爲480f //縮放比。因爲是固定比例縮放,只用高或者寬其中一個數據進行計算便可 int be = 1;//be=1表示不縮放 if (w > h && w > ww) {//若是寬度大的話根據寬度固定大小縮放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {//若是高度高的話根據寬度固定大小縮放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//設置縮放比例 //從新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了 bitmap = BitmapFactory.decodeFile(imagefile, newOpts); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質量壓縮方法,這裏100表示不壓縮,把壓縮後的數據存放到baos中 int options = 50; while ( (baos.toByteArray().length / 1024) > 100) { //循環判斷若是壓縮後圖片是否大於100kb,大於繼續壓縮 baos.reset();//重置baos即清空baos bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裏壓縮options%,把壓縮後的數據存放到baos中 options -= 5;//每次都減小5 if (options == 0) { break ; } } String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()); //File file1 = new File(Environment.getExternalStorageDirectory()+"/Foodproductionofhn/ImageThumbnil/"+riOrzx+"_"+bsh+"_"+date+"_.jpg"); File file = QuZheng.getQM(riOrzx+"_"+bsh+"_"+System.currentTimeMillis(),"qz"); File file1 = QuZheng.getQM("ss","qm"); try { FileOutputStream os = new FileOutputStream(file); bitmap.compress(CompressFormat.JPEG, options, os); os.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileOutputStream os = new FileOutputStream(file1); bitmap.compress(CompressFormat.JPEG, options, os); os.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }