android 调用camera相关操作

1,通过intent调用camera

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(),
getPhotoFileName());
Log.i(TAG,"getPhotoFileName():" +getPhotoFileName());
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, GET_PHOTO_WITH_CAMERA);

private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyy-MM-dd_HH-mm-ss");
return dateFormat.format(date) + ".jpg";
}


然后onActivityResult()中

if (requestCode == GET_PHOTO_WITH_CAMERA) {
// doCropPhoto(mCurrentPhotoFile);
Uri selectedImage = imageUri;
Log.i(TAG, "uri:" + imageUri.toString());
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
BitmapDrawable bd = new BitmapDrawable(bitmap);
headerImage.setBackgroundDrawable(bd);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
}
}

2,如果要调用gallery去剪辑的话
doCropPhoto(photo)

protected void doCropPhoto(File f) {
try {
// 启动gallery去剪辑这个照片
final Intent intent = getCropImageIntent(Uri.fromFile(f));
startActivityForResult(intent, GET_PHOTO_WITH_GALLARY);
} catch (Exception e) {
}
}
public static Intent getCropImageIntent(Uri photoUri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(photoUri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);
intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
return intent;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值