編輯:關於 Android 程式設計
本篇演示一個實用場景:調起系統相機拍照,把照片保存到 SD 卡並顯示在界面上,同時拿到圖片的完整路徑,方便後續上傳。Activity 裡我這裡只貼出關鍵的事件部分代碼。
public void doPhoto(View view) { destoryBimap(); String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent, 1); } else { Toast.makeText(MainActivity.this, "沒有SD卡", Toast.LENGTH_LONG).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Uri uri = data.getData(); if (uri != null) { this.photo = BitmapFactory.decodeFile(uri.getPath()); } if (this.photo == null) { Bundle bundle = data.getExtras(); if (bundle != null) { this.photo = (Bitmap) bundle.get("data"); } else { Toast.makeText(MainActivity.this, "拍照失敗", Toast.LENGTH_LONG).show(); return; } } FileOutputStream fileOutputStream = null; try { // 獲取 SD 卡根目錄 String saveDir = Environment.getExternalStorageDirectory() + "/meitian_photos"; // 新建目錄 File dir = new File(saveDir); if (! dir.exists()) dir.mkdir(); // 生成文件名 SimpleDateFormat t = new SimpleDateFormat("yyyyMMddssSSS"); String filename = "MT" + (t.format(new Date())) + ".jpg"; // 新建文件 File file = new File(saveDir, filename); // 打開文件輸出流 fileOutputStream = new FileOutputStream(file); // 生成圖片文件 this.photo.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); // 相片的完整路徑 this.picPath = file.getPath(); ImageView imageView = (ImageView) findViewById(R.id.showPhoto); imageView.setImageBitmap(this.photo); } catch (Exception e) { e.printStackTrace(); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } } /** * 銷毀圖片文件 */ private void destoryBimap() { if (photo != null && ! photo.isRecycled()) { photo.recycle(); photo = null; } }
這裡有兩個值得注意的地方:一是拍照前先檢查 SD 卡是否已掛載;二是每次拍照前調用 destoryBimap() 回收上一張位圖,避免內存洩漏。保存時用遞增時間戳生成文件名,避免重名覆蓋。
Layout 布局頁面
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/doPhoto" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_marginBottom="10dp" android:text="拍照" android:onClick="doPhoto" /> <TextView android:id="@+id/showContent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" /> <ImageView android:id="@+id/showPhoto" android:layout_width="fill_parent" android:layout_height="250dp" android:scaleType="centerCrop" android:src="@drawable/add" android:layout_marginBottom="10dp" /> </LinearLayout> </ScrollView> </LinearLayout>
拿到 this.picPath 之後,就可以把它交給網絡工具類完成上傳。整體流程就是:檢查存儲 → 調起相機 → 保存並顯示 → 記錄路徑以備上傳。
Android App 內監聽截圖並擴充分享內容的實作思路
截圖是使用者很常做的操作,若 App 能在使用者截圖後即時反應,例如加上二維碼、浮水印或分享提示,就能延伸更多互動。由於 Android 沒有直接提供截圖通知 API,因
Android 6.0 權限管理實作:targetSdkVersion ≥ 23 該做的事
當 App 跑在 Android 6.0 以上的手機,且 targetSdkVersion >= 23 時,使用較敏感的權限就必須先取得使用者同意——讀寫 SD 卡
自定義控件實戰(四):打造通訊錄字母快速索引欄 QuickIndexBar
自定義View通訊錄字母快速索引 在 Android 日常開發中,我們經常在聯繫人界面看到側邊的字母導航欄。當用戶點擊某個字母時,列表會依據漢字的首字母拼音快速定位到對
Android 動態生成九宮格版面,實作多圖片上傳
動態版面 + 多圖上傳,一次搞定 本文示範如何在 Android 中不寫死 XML,而是用程式碼動態排出九宮格、補上分隔線與「+」號,再搭配非同步任務把多張圖片一次上傳。