在开发中我们可能需要使用手机拍照,为了减少工作量,我们可以直接使用Intent来启动系统自带的照相机。
使用Intent打开相机。可以指定照片的输出位置及文件名。然后在启动的Acitivity中onActivityResult获取返回的图像。
注意:如果给照相机指定了照片的输出位置。那么将接收不到返回的图像内容
activity_main.xml
<RelativeLayout 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" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回预览图像" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/button1" android:text="存储到本地" /> <ImageView android:id="@+id/imageView1" android:layout_width="320dp" android:layout_height="240dp" android:layout_below="@id/button1" /> </RelativeLayout>