一、首先导入相关权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-featureandroid:name="android.hardware.camera"android:required="true" /><uses-permission android:name="android.permission.CAMERA" /><!-- 这个权限允许应用程序录制音频。 --><uses-permission android:name="android.permission.RECORD_AUDIO" />
二、导入依赖
// CameraX 相关依赖def cameraxVersion = "1.1.0-alpha05"implementation "androidx.camera:camera-core:${cameraxVersion}"implementation "androidx.camera:camera-camera2:${cameraxVersion}"implementation "androidx.camera:camera-lifecycle:${cameraxVersion}"implementation 'androidx.camera:camera-view:1.0.0-alpha25'
三、Activity
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureException;
import androidx.camera.core.Preview;
import androidx.camera.core.VideoCapture;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;import com.google.common.util.concurrent.ListenableFuture;import org.json.JSONException;
import org.json.JSONObject;import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private static final int REQUEST_CODE_RECORD_AUDIO_PERMISSION = 1001;private static final int REQUEST_CODE_CAMERA_PERMISSION = 1002;private static final int REQUEST_