編輯:關於 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);
}
}
}
<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>
如果 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 上。
Android 快速實作一個通用 EmptyView 空視圖
好的 App 應該具備良好的互動,最好能貼心滿足使用者需求,而人性化的提醒正是其中一環。我們常在某些 App 看到這樣的場景:當內容載入失敗或抓取資料失敗時,畫面會切換成
Android 開發筆記:用列舉型別取代整數集合
問題從哪裡來 翻 Android API 會發現大量「以整數集合當參數」的設計。以 LinearLayout 為例,它的方向屬性是這樣設定的: LinearLayout.
從零開始學 Android:內部儲存(Internal Storage)實作教學(三十六)
把資料寫入內部儲存,基本步驟只有三步: 1. 呼叫 openFileOutput(),傳入檔名與操作模式,取得 FileOutputStream 物件。 2. 以 wri
Android 萬能適配器 base-adapter-helper 源碼解析
傳統 Adapter 的痛點 在 Android 開發中,ListView 與 GridView 經常需要搭配 Adapter。每次撰寫時,都要處理 convertVie