標籤:app 通過 media vertica tor imageview extern tab 定義
本Demo的實現效果是調用手機上已安裝的照相機來實現拍照的功能,拍好的照片以ImageView形式展示。
目的:學習手機調用安裝的相機照相,對大的圖片處理有所認識,這裡主要用到BitmapFactory和BitmapFactory.Options兩個類。
載入並顯示一副映像對記憶體使用量情況有顯著的影響,Android提供了一個名為BitmapFactory 的有用程式類,該程式提供了一系列的靜態方法,同意通過各種來源載入Bitmap映像。
針對我們的需求,將從檔案載入映像。並在最初的活動中顯示它。幸運的是,BitmapFactory中的可用方法將會調用BitmapFactory.Options類。這使得我們可以定義怎樣將Bitmap讀入記憶體。詳細而言,當載入映像時。可以設定BitmapFactory應該使用的採樣大小。在BitmapFactory.Options中指定inSampleSize參數。
比如。將inSampleSize = 8時。產生一幅圖的大小是原始大小的1/8。要注意的是首先應將BitmapFactoryOptions.inJustDecodeBounds變數設定為true,這將通知BitmapFactory類僅僅需返回該映像的範圍。而無需嘗試解碼映像本身。
最後將BitmapFactory.Options.inJustDecodeBounds設定為false。最後對其進行真正的解碼。
實現:
源碼:
activity_main布局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
MainActivity源碼:
package com.multimediademo1;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.widget.ImageView;public class MainActivity extends Activity {private final static int CAMERA_RESULT = 0;private ImageView imageView;private String imageFilePath;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfavoritepicture.jpg";File imageFile = new File(imageFilePath);Uri imageFileUri = Uri.fromFile(imageFile);Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);startActivityForResult(intent, CAMERA_RESULT);}@Overrideprotected void onActivityResult(int requestCode, int resultCode,Intent intent) {super.onActivityResult(requestCode, resultCode, intent);if (resultCode == RESULT_OK) {imageView = (ImageView) findViewById(R.id.imageView);Display currentDisplay = getWindowManager().getDefaultDisplay();int dw = currentDisplay.getWidth();int dh = currentDisplay.getHeight();// 載入映像的尺寸,而不是映像本身BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath, options);int heightRatio = (int) Math.ceil(options.outHeight / (float) dh);int widthRatio = (int) Math.ceil(options.outWidth / (float) dw);// 假設兩個比率都大於1。那麼映像的一條邊將大於螢幕if (heightRatio > 1 && widthRatio > 1) {if (heightRatio > widthRatio) {// 若高度比率更大,則依據它縮放options.inSampleSize = heightRatio;} else {options.inSampleSize = widthRatio;}}options.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeFile(imageFilePath, options);imageView.setImageBitmap(bitmap);}}}
源碼下載:
點擊下載原始碼
Android 使用內建的Camera應用程式捕獲映像