Android开发之调用相机拍照和调用系统相册

来源:互联网 发布:网络虚拟手机号申请 编辑:程序博客网 时间:2024/05/16 15:35
  • Android应用程序中调用相机进行拍照和选择相册中的照片是经常会使用到的,那么我们就来实现以下这个功能。

    首先创建一个工程CameraTest,布局文件一个ImageView,两个Button。
<?xml version="1.0" encoding="utf-8"?><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/iv_photo"       android:layout_gravity="center"       android:layout_width="match_parent"       android:layout_height="0dp"       android:layout_weight="1"       />    <Button        android:id="@+id/btn_photo"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="点击拍照"/>    <Button        android:id="@+id/btn_photo_album"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="从相册中选取"/></LinearLayout>
MainActivity中主要对两个按钮进行了监听,跳到相应的界面。拍照采用隐式意图来调用相机,并将拍好的照片存储在sd卡根目录。由于拍的照片可能比较大,所以在拍照之后启用了系统自带的裁剪功能,裁剪完成后加载到ImageView上。调用系统相册也同样采用隐式意图来调用,选取之后同样的对其进行裁剪,然后加载到ImageView上。
package com.example.cameratest;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.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;public class MainActivity extends AppCompatActivity {    public static final int Cut_PHOTO = 1;    public static final int SHOW_PHOTO = 2;    public static final int PHOTO_ALBUM = 3;    public static final int SHOW_PHOTO_ALBUM = 4;    private Uri imageUri;    private ImageView iv_photo;    private Button btn_photo;    private Button btn_photo_album;    private Uri uri;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iv_photo = (ImageView) this.findViewById(R.id.iv_photo);        btn_photo = (Button) this.findViewById(R.id.btn_photo);        btn_photo.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //创建File对象,用于存储选择的照片                File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");                try {                    if (outputImage.exists()) {                        outputImage.delete();                    }                    outputImage.createNewFile();                } catch (IOException e) {                    e.printStackTrace();                }                imageUri = Uri.fromFile(outputImage);                //隐式意图启动相机                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                // 启动相机程序                startActivityForResult(intent, Cut_PHOTO);            }        });        btn_photo_album = (Button) this.findViewById(R.id.btn_photo_album);        btn_photo_album.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 创建File对象,用于存储选择的照片                File outputImage = new File(Environment.getExternalStorageDirectory(), "outputTest.jpg");                try {                    if (outputImage.exists()) {                        outputImage.delete();                    }                    outputImage.createNewFile();                } catch (IOException e) {                    e.printStackTrace();                }                imageUri = 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, imageUri);                startActivityForResult(intent,PHOTO_ALBUM);            }        });    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        switch (requestCode) {            case Cut_PHOTO:                if (resultCode == RESULT_OK) {                    Intent intent = new Intent("com.android.camera.action.CROP");                    intent.setDataAndType(imageUri, "image/*");                    intent.putExtra("scale", true);                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                    // 启动裁剪                    startActivityForResult(intent, SHOW_PHOTO);                }                break;            case SHOW_PHOTO:                if (resultCode == RESULT_OK) {                    try {                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));                        // 将裁剪后的照片显示出来                        iv_photo.setImageBitmap(bitmap);                    } catch (FileNotFoundException e) {                        e.printStackTrace();                    }                }                break;            case PHOTO_ALBUM:                if (resultCode == RESULT_OK) {                    uri = data.getData();                    Intent intent = new Intent("com.android.camera.action.CROP");                    intent.setDataAndType(uri, "image/*");                    intent.putExtra("scale", true);                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);                    // 启动裁剪                    startActivityForResult(intent, SHOW_PHOTO_ALBUM);                }                break;            case SHOW_PHOTO_ALBUM:                if (resultCode == RESULT_OK) {                    try {                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));                        // 将裁剪后的照片显示出来                        iv_photo.setImageBitmap(bitmap);                    } catch (FileNotFoundException e) {                        e.printStackTrace();                    }                }                break;            default:                break;        }    }}
当然,别忘了加对sd卡操作的权限。
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
由于要调用相机进行拍照,所以我选择运行在真机上进行测试。将程序运行在小米3上,界面如下。

效果

点击拍照按钮,我拍了张照片,点击 "√" 之后跳到如下界面。

裁剪

调整好裁剪的大小之后点击应用,发现我们刚才裁剪的照片已经加载到ImageView上了。

效果

然后点击从相册中选取按钮,我选了张美女图片,如下,它已经跳到了裁剪界面。

美女

选好裁剪效果之后点击应用。效果就出来了。

美女效果

从相册中选取照片的时候可能会加载不上,如果出现这个问题,在清单文件中<activity>节点下,配置不要开启硬件加速。
android:hardwareAccelerated="false"
在实际的应用开发中加载图片使用Bitmap的时候要对图片进行缩放,不然很容易就会造成OOM了。如有希望了解Bitmap加载图片请点击

Android开发之Bitmap加载大图

拍照和相册选取的功能我们就完成了。如有错误请指出,谢谢!
1 1