編輯:關於 Android 程式設計
先來看看效果圖:

Layout 布局如下:
這裡我們把本地 SD 根目錄下的一張圖片轉成 Bitmap,再存到要上傳的 tmp 資料夾中:
private class LoadPhotoTask extends AsyncTask{ private Bitmap mLoadedBitmap = null; protected Boolean doInBackground(Void... params) { try { if(mFilePathName.length() > 0){ File file = new File(mFilePathName); if (!file.exists()) { return false; } BitmapFactory.Options opt = new BitmapFactory.Options(); long fileSize = file.length(); int maxSize = 2*1024 * 1024; if(fileSize <= maxSize){ opt.inSampleSize = 1; }else if(fileSize <= maxSize * 4){ //小於8M opt.inSampleSize = 2; }else{ long times = fileSize / maxSize; opt.inSampleSize = (int)(Math.log(times) / Math.log(2.0)) + 1; //Math.log返回以e為底的對數 } try{ mLoadedBitmap = BitmapFactory.decodeFile(mFilePathName,opt);//opt為縮小的倍數 mUploadFilePathName = SaveBitmapToFile(mLoadedBitmap); }catch(OutOfMemoryError e){ Toast.makeText(UploadPhotoActivity.this, getResources().getString(R.string.no_memory_to_view_photo), Toast.LENGTH_SHORT).show(); UploadPhotoActivity.this.finish(); } } return true; } catch (Exception e) { Log.e("UploadPhotoActivity", "doInBackground", e); return false; } } protected void onPostExecute(Boolean result){ try { showLoadPreviewProgressBar(false); if(mLoadedBitmap != null){ ImageView IamgePreView = (ImageView)findViewById(R.id.photo_upload_preview_image); IamgePreView.setImageBitmap(mLoadedBitmap); }else{ } mLoadedBitmap = null; } catch (Exception e) { Log.e("UploadPhotoActivity", "onPostExecute", e); } } }
將 Bitmap 儲存為檔案的程式碼:
private String SaveBitmapToFile(Bitmap bmp){ if (null == bmp) { return null; } String fileName = "upload_tmp.jpg"; File f = this.getFileStreamPath(fileName);//data/data/com.example.tianqitongtest/files/upload_tmp.jpg,這個是要上傳的文件存的位置 if (f.exists()) { f.delete(); } FileOutputStream ostream; try { int targetWidth = 780; int w = bmp.getWidth(); if (w > targetWidth) { int h = bmp.getHeight(); int targetHeight = (targetWidth * h) / w; bmp = Bitmap.createScaledBitmap(bmp, targetWidth, targetHeight, true);//根據指定寬度和高度來生成一個新的Bitmap } ostream = this.openFileOutput(fileName, MODE_PRIVATE); bmp.compress(Bitmap.CompressFormat.JPEG, 70, ostream); ostream.flush(); ostream.close(); ostream = null; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return f.getAbsolutePath(); }
上傳時需要把螢幕鍵盤收起:
private void hideInputMethod(){ View view = getCurrentFocus(); if(view != null){ ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } }
取得 SD 卡上 11.jpg 圖片的路徑:
private String getStoredPicPath() { String fileName = "11.jpg"; return Environment.getExternalStorageDirectory()+"/"+fileName; }
初探Android N有趣新特性:Notification通知的那些好用功能
本文基於 Android L 官方 Sample「LNotifications」進行源碼分析,因源碼涉及相關資源,程式碼與個人註解已整理上傳至 Git。 先整理一下我的收
AChartEngine 圖表框架使用心得
前言 AChartEngine 是一個常見的 Android 圖表框架,可用來建立折線圖、長條圖、圓餅圖等多種圖表。本文以折線圖為例,整理使用時的核心概念與實作心得。
Android Activity 螢幕方向鎖定與旋轉時的資料保存機制詳解
現在的手機多半內建方向感應器,螢幕會依姿勢自動橫豎切換(前提是未鎖定方向)。但有時我們的應用只允許在單一方向下運作,這時就得鎖定 Activity 的螢幕方向。另一個常見
React Native for Android 環境搭建與第一個 App 完整實錄
一、為什麼值得投入 Facebook 先開源了 React Native for iOS,並在 2015 年 9 月 15 日補上了 Android 版本。雖然 Andr