Intent拍照
<span style="font-size:18px;">String photoPath = "";
public void doTakePhoto() {
Intent intent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File parent = FileUitlity.getInstance(this).makeDir("jredu_head");
// 路径
photoPath = parent.getPath() + File.separator
+ System.currentTimeMillis() + ".png";
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(photoPath)));
startActivityForResult(intent, TAKE_FROM_CAPTURE);
}</span>
从相册中选取
<span style="font-size:18px;color:#333333;">public void doPhonePhoto() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, TAKE_FROM_ALBULE);
}</span>
裁剪方法:
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 设置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, RESULT_PHOTO);
}
重写onActivityResult()方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case TAKE_FROM_CAPTURE:
//裁剪
startPhotoZoom(Uri.fromFile(new File(photoPath)));
break;
case TAKE_FROM_ALBULE:
Cursor cursor = this.getContentResolver().query(data.getData(),new String[] { MediaStore.Images.Media.DATA }, null, null,
null);
cursor.moveToFirst();
photoPath = cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
//裁剪
startPhotoZoom(Uri.fromFile(new File(photoPath)));
// image.setImageURI(Uri.fromFile(new File(photoPath)));
break;
case RESULT_PHOTO:
Bundle bundle=data.getExtras();
if(bundle!=null){
Bitmap b=bundle.getParcelable("data");
image.setImageBitmap(b);//展示
}
break;
}
}
拍照需要权限
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>