相机
// 打开相机
Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
startActivityForResult(it, 1000);
if (requestCode == 1000) {
Intent it = new Intent("com.android.camera.action.CROP");
it.setDataAndType(Uri.fromFile(new File(path)), "image/*");
it.putExtra("crop", true);
// 截图框的宽高比
it.putExtra("aspactX", 1);
it.putExtra("aspactY", 1);
// 图片的宽高
it.putExtra("outputX", 250);
it.putExtra("outputY", 250);
it.putExtra("return-data", true);
startActivityForResult(it, 2000);
}
if (requestCode == 2000) {
Bitmap bitmap = data.getParcelableExtra("data");
iv.setImageBitmap(bitmap);
}
相册
Intent in = new Intent(Intent.ACTION_PICK);
in.setType("image/*");
startActivityForResult(in, 10);
if (requestCode == 10) {
Intent in = new Intent("com.android.camera.action.CROP");
Uri uri = data.getData();
in.setDataAndType(uri, "image/*");
in.putExtra("crop", true);
// 截图框的宽高比
in.putExtra("aspactX", 1);
in.putExtra("aspactY", 1);
// 图片的宽高
in.putExtra("outputX", 250);
in.putExtra("outputY", 250);
in.putExtra("return-data", true);
startActivityForResult(in, 20);
}
if (requestCode == 20) {
Bitmap bitmap = data.getParcelableExtra("data");
iv.setImageBitmap(bitmap);
}