android-拍照回调

第一步:设置权限

 <!-- 相机权限 -->
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.CAMERA" />

第二本部调用拍照方法:

private void takePic() {

//生成一个文件名字

// 获取以当前时间为命名方式的图片

String fileName = generateFileName(".png");
newAddImgName = fileName;
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Uri imageUri = Uri.fromFile(new File(fileName));

//床底之后进行拍照


openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
openCameraIntent.putExtra("return-data", true);
startActivityForResult(openCameraIntent, REQUEST_TAKE_PICTURE);
}


// 获取当前时间的图片
private String generateFileName(String type) {
String fileName = "" + System.currentTimeMillis() + type;
return FileUtils.getCachePath("gen") + fileName;

}

//第三部:完成拍照之后回调

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_TAKE_PICTURE:

//显示
showNewAddPic();
break;
default:
break;
}
}
}


private void showNewAddPic() {


ImageView imageView = (ImageView) getLayoutInflater().inflate(
R.layout.added_iv, null);
try {

//在一个内嵌的linerlayout中进行生成一张图片
int heightWidth = getResources().getDimensionPixelSize(
R.dimen.added_img_size);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
heightWidth, heightWidth);
int margin = getResources().getDimensionPixelSize(
R.dimen.added_img_margin);
layoutParams.setMargins(margin, margin, margin, margin);
pictureLin.addView(imageView, pictureLin.getChildCount() - 1,
layoutParams);
imageView.setTag(newAddImgName);
// imageView本身的设置才是最关键的
imageView.setImageBitmap(ImageUtils.getSmallBitmap(newAddImgName,
heightWidth, heightWidth));// imageView width is 0 intime,so
// use heightWidth

//长按删除,单按查看
imageView.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showPicture(newAddImgName);


}
});


imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showDeleteImgConfirmDialog(v);
return true;
}
});
chargeAddBtn();
} catch (Exception e) {
}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值