android 点击按钮调用系统相机并打开预览窗口
时间: 2023-12-15 12:05:27 浏览: 42
在 Android 中,你可以通过调用系统相机来捕捉照片和视频。下面是一个简单的示例代码,可用于打开系统相机并预览拍摄:
```
private static final int REQUEST_IMAGE_CAPTURE = 1;
// 相机预览
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
// 处理返回结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
// 将照片显示在 ImageView 中
mImageView.setImageBitmap(imageBitmap);
}
}
```
在上面的代码中,`dispatchTakePictureIntent()` 方法调用系统相机并打开预览窗口。通过调用 `startActivityForResult()` 方法,我们可以等待系统相机完成任务并返回结果。在 `onActivityResult()` 方法中,我们可以获取拍摄的照片并将其显示在 ImageView 中。
请注意,这只是一个简单的示例代码,仅用于演示如何打开系统相机并预览拍摄。在实际应用中,你需要考虑更多的细节和错误处理。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)