固然,在捕获一张照片时,若是Camera程序没有将图片返回给调用活动,那么简单的使用内置的Camera应用程序捕获图像将不具备真正的做用。而为了使用它真正有用,能够将活动中的startActivity方法替换成startActivityForResult方法。使用该方法将容许咱们访问从Camera应用程序中返回的数据,它刚好是用户以位图(Bitmap)形式捕获的图像。android
一下是一个基本的示例。编程
1 package com.bluemobi.nthm.showversion; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.graphics.Bitmap; 6 import android.os.Bundle; 7 import android.widget.ImageView; 8 9 public class CameraIntent extends Activity { 10 private final static int CAMERA_RESULT=0; 11 private ImageView imv; 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.cameraintentactivity); 16 17 Intent i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 18 startActivityForResult(i, CAMERA_RESULT); 19 } 20 @Override 21 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 22 super.onActivityResult(requestCode, resultCode, data); 23 if(resultCode==RESULT_OK){ 24 Bundle extras=data.getExtras(); 25 Bitmap bmp=(Bitmap) extras.get("data"); 26 27 imv=(ImageView) findViewById(R.id.ReturnedImageView); 28 imv.setImageBitmap(bmp); 29 } 30 } 31 32 }
它须要在项目的layout/cameraintentactivity.xml 文件中添加以下的内容:app
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ImageView 8 android:id="@+id/ReturnedImageView" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:contentDescription="@string/about_us"/> 12 13 </LinearLayout>
为了完成上述示例,一下是AndroidManifest.xml文件的内容。ide
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.bluemobi.nthm.showversion" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="11" android:targetSdkVersion="20" /> 9 10 <application 11 android:icon="@drawable/ic_launcher" 12 android:label="@string/app_name"> 13 <activity 14 android:name=".CameraIntent" 15 android:label="@string/app_name" > 16 <intent-filter> 17 <action android:name="android.intent.action.MAIN" /> 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 </application> 22 </manifest>
在此示例中,Camera应用程序在一个经过意图传递的附加值(extra)中返回图像,而该意图将在onActivityResult方法中传递给主调活动。附加值的名称“data”,它包含一个Bitmap对象,须要从泛型对象将它强制转换过来。布局
1 //从意图中获取附加值 2 Bundle extras=data.getExtras(); 3 //从附加值中获取返回的对象 4 Bitmap bmp=(Bitmap) extras.get("data");
在咱们的布局XML(layout/cameraintentactivity.xml)文件中,有一个ImageView对象。该ImageView是泛型视图的扩展,其支持图像的显示。因为咱们有一个带有指定ReturnedImageView编号(id)的ImageView对象,所以须要在活动中得到它的引用,并经过setImageBitmap方法将它的Bitmap对象设置为返回的图像。这将使得应用程序用户可以查看这幅捕获的图像。spa
为了得到ImageView对象的引用,使用在Activity类中指定的标准方法findViewById。该方法使得咱们可以以编程的方式引用在布局XML文件中指定的元素,咱们正在经过将元素id传递给setContentView来使用布局XML文件。上述示例在XML中以如下方式指定ImageView对象:设计
1 <ImageView 2 android:id="@+id/ReturnedImageView" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:contentDescription="@string/about_us"/>
为了引用ImageView并通知它显示来自Camera的Bitmap对象,使用如下代码。code
1 imv=(ImageView) findViewById(R.id.ReturnedImageView); 2 imv.setImageBitmap(bmp);
当运行这个示例时,你可能会注意到结果图像很小。这不是一个bug——相反这是一个通过精心设计的。当经过一个意图触发时,Camera应用程序不会将全尺寸的图像返回给主调活动。一般,这样作须要大量的内存,而移动设备通常内存方面受限。相反,Camera应用程序将在返回一幅很小的缩略图。xml