Android studio调用系统拍照的详细教程
时间: 2023-06-09 20:05:58 浏览: 103
好的,以下是Android Studio调用系统拍照的详细教程:
1. 首先在AndroidManifest.xml文件中添加如下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
```
2. 在XML布局文件中添加一个ImageView用于显示拍摄的照片,一个Button用于触发拍照功能:
```xml
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/capture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"/>
```
3. 在Activity或Fragment中查找ImageView和Button:
```java
private ImageView mImageView;
private Button mCaptureButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.image_view);
mCaptureButton = findViewById(R.id.capture_button);
}
```
4. 在mCaptureButton的点击事件中调用系统相机:
```java
private static final int REQUEST_IMAGE_CAPTURE = 1;
mCaptureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
});
```
5. 处理拍摄照片返回的结果,在onActivityResult()方法中获取拍摄照片的Bitmap对象并显示在ImageView中:
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
```
以上就是Android Studio调用系统拍照的详细教程,希望对你有所帮助!