调用摄像头拍照并显示

package com.example.choosepictest;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
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 TAKE_PHOTO = 1;

    public  static final int CROP_PHOTO = 2 ;

    private Button takephoto;

    private ImageView picture ;

    private Uri imageUri ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        takephoto = (Button) findViewById(R.id.take_phone);
        picture = (ImageView) findViewById( R.id.picture) ;
        takephoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建File对象,用于存储拍照后的照片
                File outputImage = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");
                System.out.println(Environment.getExternalStorageDirectory());
                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 , 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");//为系统剪裁的Action

                    intent.setDataAndType(imageUri ,"image/*"); //设置剪裁的属性类型
                    intent.putExtra("scale",true);//scale为缩放的意思

                    intent.putExtra(MediaStore.EXTRA_OUTPUT , imageUri);
                    startActivityForResult(intent , CROP_PHOTO); //需要返回结果来判断是否截图成功的故用这个方法

                }
                break;
            case CROP_PHOTO:
                if(resultCode==RESULT_OK){
                    try{

                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));//而contentresolver则是用于管理所有程序的contentprovider实例
                        picture.setImageBitmap(bitmap);
                    }catch (FileNotFoundException e){
                        e.printStackTrace();
                    }
                }
                break;
            default:break;
        }
    }
}

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<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"
   >
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Take photo"
    android:id="@+id/take_phone"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/picture"
        android:layout_gravity="center_vertical"/>
LinearLayout>

总结:1 Environment.getExternalStorageDirectory() 得到SD卡的根目录
2 剪裁照片的属性设置
3 解析成位图

你可能感兴趣的:(Android)