第一步:设置权限
<!-- 相机权限 -->
<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("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) {
}
}