Android 调用相机拍照,适配到 Android 10
private void checkPermissionAndCamera() {int hasCameraPermission = ContextCompat.checkSelfPermission(getApplication(),Manifest.permission.CAMERA);if (hasCameraPermission == PackageManager.PERMISSION_GRANTED) {//有调起相机拍照。openCamera();} else {//没有权限,申请权限。ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},PERMISSION_CAMERA_REQUEST_CODE);}}
/**
处理权限申请的回调。*/@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {if (requestCode == PERMISSION_CAMERA_REQUEST_CODE) {if (grantResults.length > 0&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {//允许权限,有调起相机拍照。openCamera();} else {//拒绝权限,弹出提示框。Toast.makeText(this,"拍照权限被拒绝",Toast.LENGTH_LONG).show();}}}
申请权限后,就可以调起相机拍照了。调用相机只需要调用 startActivityForResult 传一个 Intent 就可以了,但是这个 Intent 需要传递一个 uri,用于保存拍出来的图片,创建这个 uri 时,各个 Android 版本有所不同,需要进行版本兼容。
//用于保存拍照图片的 uriprivate Uri mCameraUri;
// 用于保存图片的文件路径,Android 10 以下使用图片路径访问图片 private String mCameraImagePath;
// 是否是 Android 10 以上手机 private boolean isAndroidQ = Build.VERSION.S
DK_INT >= android.os.Build.VERSION_CODES.Q;
/**
调起相机拍照*/private void openCamera() {Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 判断是否有相机 if (captureIntent.resolveActivity(getPackageManager()) != null) {File photoFile = null;Uri photoUri = null;
if (isAndroidQ) {// 适配 android 10photoUri = createImageUri();} else {try {photoFile = createImageFile();} catch (IOException e) {e.printStackTrace();}
if (photoFile != null) {mCameraImagePath = photoFile.getAbsolutePath();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//适配 Android 7.0 文件权限,通过 FileProvider 创建一个 content 类型的 UriphotoUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", photoFile);} else {photoUri = Uri.fromFile(photoFile);}}}
mCameraUri = photoUri;if (photoUri != null) {captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);startActivityForResult(captureIntent, CAMERA_REQUEST_CODE);}}}
/**
创建图片地址 uri,用于保存拍照后的照片 Android 10 以后使用这种方法*/private Uri createImageUri() {String status = Environment.getExternalStorageState();// 判断是否有 SD 卡,优先使用 SD 卡存储,当没有 SD 卡时使用手机存储 if (status.equals(Environment.MEDIA_MOUNTED)) {return getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());} else {return getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, new ContentValues());}}
/**
创建保存图片的文件*/private File createImageFile() throws IOException {String imageName = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);if (!storageDir.exists()) {storageDir.mkdir();}File tempFile = new File(storageDir, imageName);if (!Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(tempFile))) {return null;}return tempFile;}
接收拍照结果和显示图片
@Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == CAMERA_REQUEST_CODE) {if (resultCode == RESULT_OK) {if (isAndroidQ) {// Android 10 使用图片 uri 加载 ivPhoto.setImageURI(mCameraUri);} else {// 使用图片路径加载 ivPhoto.setImageBitmap(BitmapFactory.decodeFile(mCameraImagePath));}} else {Toast.makeText(this,"取消",Toast.LENGTH_LONG).show();}}}
评论