很多应用程序都可能会使用到调用摄像头拍照的功能,比如说程序里需要上传一张图片作为用户的头像,这时打开摄像头拍张照是最简单快捷的。下面就让我们通过一个例子来学
activity 的代码
同样创建了一个File对象,用于存储从相册中选择的图片。然后构建出一个 Intent 对象,并将它的 action 指定为android.intent.action.GET_CONTENT。接着给这个Intent对象设置一些必要的参数,包括是否允许缩放和裁剪、图片的输出位置等。最后调用startActivityForResult()方法,就可以打开 相册程序选择照片了。
习一下,如何才能在应用程序里调用手机的摄像头进行拍照。
这个是布局文件
<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"
tools:context="com.jsruyun.opencamearandc.MainActivity" >
<Button
android:id="@+id/btn_open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开照相机" />
<ImageView
android:id="@+id/image_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null" />
</LinearLayout>
activity 的代码
package com.jsruyun.opencamearandc;
import java.io.File;
import java.io.IOException;
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.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imagePhoto;
private Button btnOpen;
public static final int TAKE_PHOTO = 1;
public static final int CROP_PHOTO = 2;
// 照片地址
private Uri photoUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagePhoto = (ImageView) findViewById(R.id.image_photo);
btnOpen = (Button) findViewById(R.id.btn_open);
// 按钮点击事件 打开照相机
btnOpen.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 创建File对象,用于存储拍照后的图片
//这里把图片命名为 tempImage.jpg 存放在手机 SD 卡的根目录下
//getExternalStorageDirectory()方法获取到的就是手机 SD卡的根目录,然后再调用Uri 的
// fromFile()方法将File对象转换成Uri对象,这个Uri对象标识着tempImage.jpg这张图片
File outputImage = new File(Environment.getExternalStorageDirectory(), "tempImage.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//调用Uri 的 fromFile()方法将File对象转换成Uri对象
photoUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//调用Intent的putExtra()方法指定图片的输出地址
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, TAKE_PHOTO); // 启动相机程序
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if(resultCode == RESULT_OK){
//
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(photoUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, CROP_PHOTO);//启动剪裁程序
}
break;
case CROP_PHOTO:
if(resultCode == RESULT_OK){
try {
//调用BitmapFactory的decodeStream()方法将tempImage.jpg这张照片解析成Bitmap对象
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(photoUri));
imagePhoto.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO: handle exception
}
}
break;
}
}
}
由于这个项目涉及到了向SD卡中写数据的操作,因此我们还需要在AndroidManifest.xml 中声明权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
以上就是拍照片 然后显示到界面上的全部过程了。
2 从相册选择照片 并且裁剪照片,代码如下所示
File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
photoUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
intent.putExtra("crop", true);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, CROP_PHOTO);
同样创建了一个File对象,用于存储从相册中选择的图片。然后构建出一个 Intent 对象,并将它的 action 指定为android.intent.action.GET_CONTENT。接着给这个Intent对象设置一些必要的参数,包括是否允许缩放和裁剪、图片的输出位置等。最后调用startActivityForResult()方法,就可以打开 相册程序选择照片了。
以上就是简单的调用照相 跟 打开相册的简单应用了。