[转]Android(安卓)调用系统摄像头
16lz
2022-04-08
在开发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();
}
}
}
}
启动摄像头的方法:
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();
}
}
}
}
更多相关文章
- Android(安卓)xxx is not translated in yyy, zzz 的解决方法
- Android使用ADB启动应用程序
- 升级Android(安卓)SDK Tools以及Android(安卓)SDK Platform-tool
- Android(安卓)Service 服务(三)—— bindService与remoteService
- Android(安卓)SDK下载和更新失败的解决方法!!!
- android SQLite 原理
- Android录音时获取分贝值的方法代码实例
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用