[转]Android 调用系统摄像头

在开发android 应用的时候我们经常需要用到摄像头来进行拍照或者是录制视频,这里介绍一种最简单的方法:调用系统摄像头来拍照,并将照片保存起来
启动摄像头的方法:
Java代码
startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), TAKE_PICTURE);


拍摄成功后接收参数的接收方法我们只需要重写
Java代码
protected void onActivityResult(int requestCode, int resultCode, Intent data)方法即可

如:

final int TAKE_PICTURE = 1;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
if (resultCode == RESULT_OK) {
Bitmap bm = (Bitmap) data.getExtras().get("data");
imgPhoto.setImageBitmap(bm);
sdCardExit = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if (sdCardExit) {
myRecAudioDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/custom_picture");

if (!myRecAudioDir.exists()) {
myRecAudioDir.mkdir();
}
} else {
mMakeTextToast("", true);
}

try {
File f = File.createTempFile(strTempFile, ".jpg",myRecAudioDir);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));

photoPath=f.getPath();

bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);

bos.flush();
bos.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

猜你喜欢

转载自yinlianghexmut.iteye.com/blog/1730452