Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 拍照顯示與路徑獲取:拍照後保存並上傳的完整流程

編輯:關於 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 之後,就可以把它交給網絡工具類完成上傳。整體流程就是:檢查存儲 → 調起相機 → 保存並顯示 → 記錄路徑以備上傳。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved