调用摄像头和相册
1.调用摄像头拍照
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Take photo" /> <ImageView android:id="@+id/picture" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> </LinearLayout>
public class MainActivity extends Activity { public static final int TAKE_PHOTO = 1; public static final int GROP_PHOTO = 2; private Button takePhoto; private ImageView picture; private Uri imagrUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); takePhoto = (Button) findViewById(R.id.take_photo); picture = (ImageView) findViewById(R.id.picture); takePhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //创建File对象,用于存储拍照后的照片 File outputImage = new File(Environment.getExternalStorageDirectory(),"tempImage.jpg"); try{ if(outputImage.exists()){ outputImage.delete(); } outputImage.createNewFile(); }catch(IOException e){ e.printStackTrace(); } imagrUri=Uri.fromFile(outputImage); Intent intent=new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imagrUri); startActivityForResult(intent, TAKE_PHOTO); //启动照相机 } }); } @Override public 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(imagrUri, "image/**"); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imagrUri); startActivityForResult(intent, GROP_PHOTO); //启动剪裁程序 } break; case GROP_PHOTO: if(resultCode == RESULT_OK){ try{ Bitmap bitmap=BitmapFactory.decodeStream( getContentResolver().openInputStream(imagrUri)); picture.setImageBitmap(bitmap); //将剪裁后的照片显示出来 }catch(FileNotFoundException e){ e.printStackTrace(); } } break; default: break; } } }
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
声明权限,向SD卡中写数据操作的声明。
MainActivity要做的第一件事是分别获得Button和ImageView的实例,并给Button注册商点击事件,然后再Button的点击事件里开始处理调用摄像头的逻辑。
首先,创建一个File对象,用于存储摄像头拍下的图片,这里把图片名字命名为tempImage.jpg,并把它存放在手机SD卡的根目录,调用Enviroment的getExternalStorageDirectory()方法获得的就是SD卡的根目录;然后调用Uri的fromFile()将File对象转换成Uri对象,这个Uri对象标志着output_image.jpg这张图片的唯一地址。
调用BitmapFactory的decodeStream()方法将图片解析成Bitmap对象,然后把他设置到ImageView中显示出来
2.从相册中选择照片
choose.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); startActivityForResult(intent,CROP_PHOTO); } });