技术与人生Android知识Android开发经验谈

调用摄像头拍照和选择照片

2016-05-25  本文已影响718人  大话程序

调用摄像头拍照


public class MainActivity extends Activity {

    public static final int TAKE_PHOTO = 1;

    public static final int CROP_PHOTO = 2;

    private Button takePhoto;

    private Button chooseFromAlbum;

    private ImageView picture;

    private Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //根据组件ID拿取组件对象
        takePhoto = (Button) findViewById(R.id.take_photo);
        chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
        picture = (ImageView) findViewById(R.id.picture);
        
        takePhoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建File对象,用于存储拍照后的照片,存储在了SD卡的根目录下
                File outputImage = new File(Environment.getExternalStorageDirectory(),
                        "output_image.jpg");
                try {
                    if (outputImage.exists()) {
                        outputImage.delete();
                    }
                    outputImage.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //将File对象转换成Uri对象,标识照片的唯一地址
                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");
                //设置Intent对象的属性
                intent.setDataAndType(imageUri, "image/*");
                intent.putExtra("scale", true);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                //启动图片剪切程序,要求返回值
                startActivityForResult(intent, CROP_PHOTO);
            }
            break;
        case CROP_PHOTO:
            if (resultCode == RESULT_OK) {
                try {
                    //调用BitmapFactory的decodeStream()方法将照片解析成Bitmap对象
                    Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
                            .openInputStream(imageUri));
                    //设置到ImageView中显示出来
                    picture.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            break;
        default:
            break;
        }
    }

}

从相册中选择照片


从相册中选择一张照片,然后进行裁剪再存储起来

添加一个按钮,设置点击逻辑

chooseFromAlbum.setOnClickListener(new OnClickListener() {                    
    @Override                                                                 
    public void onClick(View v) {  
        //存储图像文件                                           
        File outputImage = new File(Environment.getExternalStorageDirectory(),
                "output_image.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);      
        //启动相册程序,要求返回值,传入的请求码是CROP_PHOTO,选中以后可直接实现图像裁剪功能             
        startActivityForResult(intent, CROP_PHOTO);                           
    }                                                                         
});                                                                           
上一篇下一篇

猜你喜欢

热点阅读