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

Android 照相功能入門:呼叫內建 Camera 拍照並取回圖像

編輯:關於 Android 程式設計

內建的圖像擷取與儲存機制,是理解 Android 媒體能力很好的切入點,也為後續處理音訊與影片打好基礎。本文示範最基礎但也最實用的一招:直接呼叫系統內建的 Camera 應用程式拍照,再把結果取回來顯示。

核心概念

Android 鼓勵「意圖導向」的設計:你不需要自己實作相機預覽與快門邏輯,只要發出一個 ACTION_IMAGE_CAPTURE 的 Intent,系統就會幫你找到相機 App 完成拍攝,然後透過 onActivityResult 把結果交回來。這和上一篇提到的 startActivityForResult 是同一套機制。

程式碼實作

package com.example;

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    final static int CAMERA_RESULT = 0;
    ImageView imv;
    Button but;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        findViewById();

        but.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                /* 執行 Camera 內建攝像頭意圖 */
                Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i, CAMERA_RESULT);
            }
        });
    }

    private void findViewById() {
        but = (Button) findViewById(R.id.but);
        imv = (ImageView) findViewById(R.id.img);
    }

    /* Activity 執行完畢後,把拍到的照片顯示出來 */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            Bitmap bmp = (Bitmap) bundle.get("data");
            imv.setImageBitmap(bmp);
        }
    }
}

版面配置 activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true" />

    <Button
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/img"
        android:layout_centerInParent="true"
        android:text="take_a_picture" />
</RelativeLayout>

AndroidManifest.xml

如果 App 需要對外提供啟動入口,記得加上 intent-filter:

<intent-filter>
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

幾個實務提醒

這個範例走的是最簡便的路線——照片以縮圖形式放在 data.getExtras() 的 "data" 這個 key 裡。它的限制是解析度偏低,不適合當作正式用途的頭像或分享圖。

若要取得原始解析度,正確做法是在呼叫相機前先透過 MediaStore.EXTRA_OUTPUT 指定一個輸出的檔案 URI,讓相機把完整圖片寫到你的檔案裡,回到 onActivityResult 後再去讀那個檔案。同時別忘了在 manifest 中宣告 android.permission.WRITE_EXTERNAL_STORAGE(Android 6.0 以上還要處理執行期權限),並先確認外部儲存空間是否掛載可用。

另外,把大尺寸的 Bitmap 直接載入記憶體是 OOM 的常見原因,實務上應搭配 BitmapFactory.Options 的 inSampleSize 做降採樣,再顯示到 ImageView 上。

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