Android调用相机和相册

调用相机

Intent camera = new Intent();//实例化一个意图实例
camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(album,2);//设置onActivityResult消息,此处的2是随便设置的

调用相册

Intent album = new Intent(Intent.ACTION_GET_CONTENT);
album.setType("image/*");
album.setAction(MediaStore.ACTION_IMAGE_CAPTURE);  
startActivityForResult(album,2);

onActivityResult的使用

从相册中选完图片后的应用

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Uri uri = data.getData();
        Log.i("click",uri.toString());
        imageView = (ImageView)findViewById(R.id.imageView);
        try {
            Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(uri));
            imageView.setImageBitmap(bitmap);
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }

super.onActivityResult(requestCode, resultCode, data);
}

猜你喜欢

转载自blog.csdn.net/asp89007342/article/details/54318082