Android CameraX适配Android13的踩坑之路

AndroidCameraX适配Android13的踩坑之路

前言:

最近把AGP插件升级到8.1.0,新建项目的时候目标版本和编译版本都是33,发现之前的demo使用Camerax拍照和录像都失败了,于是查看了一下官网和各种资料,找到了Android13的适配方案.

行为变更:以 Android 13 或更高版本为目标平台的应用

与早期版本一样,Android 13 包含一些行为变更,这些变更可能会影响您的应用。以下行为变更仅影响以 Android 13 或更高版本为目标平台的应用。如果您的应用以 Android 13 或更高版本为目标平台,您应该修改自己的应用以适当地支持这些行为(如果适用)。

此外,请务必查看对 Android 13 上运行的所有应用都有影响的行为变更列表。

细化的媒体权限

在这里插入图片描述
该对话框的两个按钮,从上至下分别为“Allow”和“Don’t allow”
图 1. 您在请求 READ_MEDIA_AUDIO 权限时向用户显示的系统权限对话框。

如果您的应用以 Android 13 或更高版本为目标平台,并且需要访问其他应用已经创建的媒体文件,您必须请求以下一项或多项细化的媒体权限,而不是READ_EXTERNAL_STORAGE 权限:

媒体类型请求权限
图片和照片READ_MEDIA_IMAGES
视频READ_MEDIA_VIDEO
音频文件READ_MEDIA_AUDIO

如果用户之前向您的应用授予了 READ_EXTERNAL_STORAGE 权限,系统会自动向您的应用授予细化的媒体权限。否则,当应用请求上表中显示的任何权限时,系统会显示面向用户的对话框。在图 1 中,应用请求 READ_MEDIA_AUDIO 权限。

如果您同时请求 READ_MEDIA_IMAGES 权限和 READ_MEDIA_VIDEO 权限,系统只会显示一个系统权限对话框。

1.参考资料如下:

https://developer.android.google.cn/about/versions/13/features?hl=zh-cn

https://blog.csdn.net/as425017946/article/details/127530660

https://blog.csdn.net/guolin_blog/article/details/127024559

2.依赖导入:

这里的依赖都是基于AGP8.1.0,Android Studio的插件版本 Gifaffe 2022.3.1

2.1 添加统一的CameraX依赖配置:

在项目的gradle目录下新建libs.version.toml文件
在这里插入图片描述

2.2 添加CameraX依赖:

[versions]
agp = "8.1.0"
org-jetbrains-kotlin-android = "1.8.0"
core-ktx = "1.10.1"
junit = "4.13.2"
androidx-test-ext-junit = "1.1.5"
espresso-core = "3.5.1"
appcompat = "1.6.1"
material = "1.9.0"
constraintlayout = "2.1.4"
glide = "4.13.0"
glide-compiler = "4.13.0"
camerax = "1.1.0-beta03"
camerax-core = "1.1.0-beta03"
camerax-video = "1.1.0-beta03"
camerax-view = "1.1.0-beta03"
camerax-extensions = "1.1.0-beta03"
camerax-lifecycle = "1.1.0-beta03"[libraries]
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version = "1.6.1" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
glide = {group = "com.github.bumptech.glide",name = "glide",version.ref = "glide"}
camerax = {group = "androidx.camera",name = "camera-camera2",version.ref = "camerax" }
camerax-core = {group = "androidx.camera",name = "camera-core",version.ref = "camerax-core"}
camerax-video = {group = "androidx.camera",name = "camera-video",version.ref = "camerax-video"}
camerax-view = {group = "androidx.camera",name = "camera-view",version.ref = "camerax-view"}
camerax-extensions = {group = "androidx.camera",name = "camera-extensions",version.ref = "camerax-extensions"}
camerax-lifecycle = {group = "androidx.camera",name = "camera-lifecycle",version.ref = "camerax-lifecycle"}
kotlin-stdlib = {group = "org.jetbrains.kotlin",name = "kotlin-stdlib-jdk7",version.ref = "kotlin-stdlib"}
kotlin-reflect = {group = "org.jetbrains.kotlin",name = "kotlin-reflect",version.ref = "kotlin-reflect"}
kotlinx-coroutines-core = {group = "org.jetbrains.kotlin",name = "kotlinx-coroutines-core",version.ref = "kotlinx-coroutines-core"}
kotlin-kotlinx-coroutines-android = {group = "org.jetbrains.kotlin",name = "kotlinx-coroutines-androidt",version.ref = "kotlinx-coroutines-android"}
glide-compiler = {group = "com.github.bumptech.glide",name = "compiler",version.ref = "glide-compiler"}
utilcodex = {group = "com.blankj",name = "utilcodex",version.ref = "utilcodex"}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4wxQEzPo-1692190126584)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230816194910526.png)]

2.3 在app的build.gradle目录下引入依赖:

dependencies {implementation(libs.core.ktx)implementation(libs.appcompat)implementation(libs.material)implementation(libs.constraintlayout)implementation(libs.glide)implementation(libs.camerax)implementation(libs.camerax.core)implementation(libs.camerax.view)implementation(libs.camerax.extensions)implementation(libs.camerax.lifecycle)implementation(libs.camerax.video)implementation(libs.kotlin.stdlib)implementation(libs.kotlin.reflect)implementation(libs.utilcodex)testImplementation(libs.junit)androidTestImplementation(libs.androidx.test.ext.junit)androidTestImplementation(libs.espresso.core)annotationProcessor(libs.glide.compiler)
}

在这里插入图片描述

3.主界面布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><androidx.camera.view.PreviewViewandroid:id="@+id/mPreviewView"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/btnCameraCapture"android:layout_width="0dp"android:layout_height="50dp"android:layout_marginBottom="50dp"android:background="@color/colorPrimaryDark"android:text="拍照"android:textColor="@color/white"android:textSize="16sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@+id/btnVideo" /><Buttonandroid:id="@+id/btnVideo"android:layout_width="0dp"android:layout_height="50dp"android:layout_marginStart="10dp"android:layout_marginBottom="50dp"android:background="@color/colorPrimaryDark"android:text="录像"android:textColor="@color/white"android:textSize="16sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toRightOf="@+id/btnCameraCapture"app:layout_constraintRight_toLeftOf="@+id/btnSwitch" /><Buttonandroid:id="@+id/btnSwitch"android:layout_width="0dp"android:layout_height="50dp"android:layout_marginStart="10dp"android:layout_marginBottom="50dp"android:background="@color/colorPrimaryDark"android:gravity="center"android:text="切换镜头"android:textColor="@color/white"android:textSize="16sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toRightOf="@+id/btnVideo"app:layout_constraintRight_toRightOf="parent" /><Buttonandroid:id="@+id/btnOpenCamera"android:layout_width="200dp"android:layout_height="50dp"android:background="@color/colorPrimaryDark"android:text="进入相机拍照界面"android:textColor="@color/white"android:textSize="16sp"tools:ignore="MissingConstraints" /></androidx.constraintlayout.widget.ConstraintLayout>

4.选择相机界面布局如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"><Buttonandroid:id="@+id/btnCamera"android:layout_width="0dp"android:layout_height="wrap_content"android:text="打开相机"tools:ignore="MissingConstraints" /><ImageViewandroid:id="@+id/iv_avatar"android:layout_width="100dp"android:layout_height="100dp"android:layout_marginStart="20dp"android:layout_marginTop="20dp"android:background="@mipmap/ic_launcher"app:layout_constraintLeft_toRightOf="@+id/btnCamera"app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

5.Android13权限适配:

Android13相较于之前的版本变化很大,细化了权限请求,具体的参考上面的官网资料,直接上代码:

5.1 Android13之前的适配:

可以看到,API 32也就是Android 12及以下系统,我们仍然声明的是READ_EXTERNAL_STORAGE权限。

在这里插入图片描述

5.2 Android13及以上的适配:

从Android 13开始,我们就会使用READ_MEDIA_IMAGES、READ_MEDIA_VIDEO、READ_MEDIA_AUDIO来替代了。

<!--存储图像或者视频权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"tools:ignore="ScopedStorage" android:maxSdkVersion="32"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"android:maxSdkVersion="32"tools:ignore="ScopedStorage" />
<!--录制音频权限-->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>

6.项目中简单适配:

6.1 Android13之前权限请求如下:

@SuppressLint("RestrictedApi")
private fun initPermission() {if (allPermissionsGranted()) {// ImageCapturestartCamera()} else {ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, Constants.REQUEST_CODE_PERMISSIONS)}}private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED}override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults:IntArray) {super.onRequestPermissionsResult(requestCode, permissions, grantResults)if (requestCode == Constants.REQUEST_CODE_PERMISSIONS) {if (allPermissionsGranted()) {startCamera()} else {ToastUtils.shortToast("请您打开必要权限")}}}

6.2 Android13及以上的版本权限请求适配如下:

private fun initPermission() {if (checkPermissions()) {// ImageCapturestartCamera()} else {requestPermission()}
}/*** Android13检查权限进行了细化,每个需要单独申请,这里我有拍照和录像,所以加入相机和录像权限***/
private fun checkPermissions(): Boolean {when {Build.VERSION.SDK_INT >= 33 -> {val permissions = arrayOf(Manifest.permission.READ_MEDIA_IMAGES,Manifest.permission.READ_MEDIA_AUDIO,Manifest.permission.READ_MEDIA_VIDEO,Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO,)for (permission in permissions) {return Environment.isExternalStorageManager()}}else -> {for (permission in REQUIRED_PERMISSIONS) {if (ContextCompat.checkSelfPermission(this,permission) != PackageManager.PERMISSION_GRANTED) {return false}}}}return true}/*** 用户拒绝后请求权限需要同时申请,刚开始我是单独申请的调试后发现一直报错,所以改为一起申请***/private fun requestPermission() {when {Build.VERSION.SDK_INT >= 33 -> {ActivityCompat.requestPermissions(this,arrayOf(Manifest.permission.READ_MEDIA_IMAGES,Manifest.permission.READ_MEDIA_AUDIO,Manifest.permission.READ_MEDIA_VIDEO,Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO),Constants.REQUEST_CODE_PERMISSIONS)}else -> {ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, Constants.REQUEST_CODE_PERMISSIONS)}}}/***用户请求权限后的回调,这里我是测试demo,所以用户拒绝后我会重复请求,真实项目根自己的需求来动态申请***/override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults:IntArray) {super.onRequestPermissionsResult(requestCode, permissions, grantResults)when (requestCode) {Constants.REQUEST_CODE_PERMISSIONS -> {var allPermissionsGranted = truefor (result in grantResults) {if (result != PackageManager.PERMISSION_GRANTED) {allPermissionsGranted = falsebreak}}when {allPermissionsGranted -> {// 权限已授予,执行文件读写操作startCamera()}else -> {// 权限被拒绝,处理权限请求失败的情况ToastUtils.shortToast("请您打开必要权限")requestPermission()}}}}}

7.拍照和录像代码:

7.1 拍照:

/*** 开始拍照*/
private fun takePhoto() {val imageCapture = imageCamera ?: returnval photoFile = createFile(outputDirectory, DATE_FORMAT, PHOTO_EXTENSION)val metadata = ImageCapture.Metadata().apply {// Mirror image when using the front cameraisReversedHorizontal = lensFacing == CameraSelector.LENS_FACING_FRONT}val outputOptions =ImageCapture.OutputFileOptions.Builder(photoFile).setMetadata(metadata).build()imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this),object : ImageCapture.OnImageSavedCallback {override fun onError(exc: ImageCaptureException) {LogUtils.e(TAG, "Photo capture failed: ${exc.message}", exc)ToastUtils.shortToast(" 拍照失败 ${exc.message}")}override fun onImageSaved(output: ImageCapture.OutputFileResults) {val savedUri = output.savedUri ?: Uri.fromFile(photoFile)ToastUtils.shortToast(" 拍照成功 $savedUri")LogUtils.e(TAG, savedUri.path.toString())val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(savedUri.toFile().extension)MediaScannerConnection.scanFile(this@MainActivity,arrayOf(savedUri.toFile().absolutePath),arrayOf(mimeType)) { _, uri ->LogUtils.d(TAG,"Image capture scanned into media store: ${uri.path.toString()}")}}})
}

7.2 录像:

之前的版本很老还在测试阶段,所以官方api发生了一些改变:

旧的api示例如下:

/*** 开始录像*/
@SuppressLint("RestrictedApi", "ClickableViewAccessibility")
private fun takeVideo() {isRecordVideo = trueval mFileDateFormat = SimpleDateFormat(DATE_FORMAT, Locale.US)//视频保存路径val file = File(FileManager.getCameraVideoPath(), mFileDateFormat.format(Date()) + ".mp4")//开始录像videoCapture?.startRecording(file,Executors.newSingleThreadExecutor(),object : OnVideoSavedCallback {override fun onVideoSaved(@NonNull file: File) {isRecordVideo = falseLogUtils.d(TAG,"===视频保存的地址为=== ${file.absolutePath}")//保存视频成功回调,会在停止录制时被调用ToastUtils.shortToast(" 录像成功 $file")}override fun onError(videoCaptureError: Int, message: String, cause: Throwable?) {//保存失败的回调,可能在开始或结束录制时被调用isRecordVideo = falseLogUtils.e(TAG, "onError: $message")ToastUtils.shortToast(" 录像失败 $message")}})
}

新的api示例如下:

startRecording方法参数发生了一些变化:第一个参数是传入一个文件输出信息类,之前是直接传入文件,其实影响不大

我们通过val outputOptions = OutputFileOptions.Builder(file)这个类构建一个对象,然后在开始录像时传入即可.

/*** 开始录像*/
@SuppressLint("RestrictedApi", "ClickableViewAccessibility", "MissingPermission")
private fun takeVideo() {//开始录像try {isRecordVideo = trueval mFileDateFormat = SimpleDateFormat(DATE_FORMAT, Locale.US)//视频保存路径val file =File(FileManager.getCameraVideoPath(), mFileDateFormat.format(Date()) + ".mp4")val outputOptions = OutputFileOptions.Builder(file)videoCapture?.startRecording(outputOptions.build(),Executors.newSingleThreadExecutor(),object : OnVideoSavedCallback {override fun onVideoSaved(outputFileResults: VideoCapture.OutputFileResults) {isRecordVideo = falseLogUtils.d(TAG, "===视频保存的地址为=== ${file.absolutePath}")//保存视频成功回调,会在停止录制时被调用ToastUtils.shortToast(" 录像成功 $file")}override fun onError(videoCaptureError: Int,message: String,cause: Throwable?) {//保存失败的回调,可能在开始或结束录制时被调用isRecordVideo = falseLogUtils.e(TAG, "onError: $message")ToastUtils.shortToast(" 录像失败 $message")}})} catch (e: Exception) {e.printStackTrace()LogUtils.e(TAG, "===录像出错===${e.message}")}
}

7.3 停止录像:

这里通过isRecordVideo是否正在录像进程录像和停止录像的操作

btnVideo.setOnClickListener {if (!isRecordVideo) {takeVideo()isRecordVideo = truebtnVideo.text = "停止录像"} else {isRecordVideo = falsevideoCapture?.stopRecording()//停止录制//preview?.clear()//清除预览btnVideo.text = "开始录像"}
}

8.常量工具类:

object Constants {const val REQUEST_CODE_PERMISSIONS = 101const val REQUEST_CODE_CAMERA = 102const val REQUEST_CODE_CROP = 103const val DATE_FORMAT = "yyyy-MM-dd HH-mm-ss"const val PHOTO_EXTENSION = ".jpg"val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO)
}

9.ToastUtils:

package com.example.cameraxdemo.utilsimport android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import android.view.Gravity
import android.widget.Toast
import androidx.annotation.StringRes
import com.example.cameraxdemo.app.CameraApp
import java.lang.reflect.Field/***@author: njb*@date:   2023/8/15 17:13*@desc:*/
object ToastUtils {private const val TAG = "ToastUtil"private var mToast: Toast? = nullprivate var sField_TN: Field? = nullprivate var sField_TN_Handler: Field? = nullprivate var sIsHookFieldInit = falseprivate const val FIELD_NAME_TN = "mTN"private const val FIELD_NAME_HANDLER = "mHandler"private fun showToast(context: Context, text: CharSequence,duration: Int, isShowCenterFlag: Boolean) {val toastRunnable = ToastRunnable(context, text, duration, isShowCenterFlag)if (context is Activity) {if (!context.isFinishing) {context.runOnUiThread(toastRunnable)}} else {val handler = Handler(context.mainLooper)handler.post(toastRunnable)}}fun shortToast(context: Context, text: CharSequence) {showToast(context, text, Toast.LENGTH_SHORT, false)}fun longToast(context: Context, text: CharSequence) {showToast(context, text, Toast.LENGTH_LONG, false)}fun shortToast(msg: String) {showToast(CameraApp.mInstance, msg, Toast.LENGTH_SHORT, false)}fun shortToast(@StringRes resId: Int) {showToast(CameraApp.mInstance, CameraApp.mInstance.getText(resId),Toast.LENGTH_SHORT, false)}fun centerShortToast(msg: String) {showToast(CameraApp.mInstance, msg, Toast.LENGTH_SHORT, true)}fun centerShortToast(@StringRes resId: Int) {showToast(CameraApp.mInstance, CameraApp.mInstance.getText(resId),Toast.LENGTH_SHORT, true)}fun cancelToast() {val looper = Looper.getMainLooper()if (looper.thread === Thread.currentThread()) {mToast!!.cancel()} else {Handler(looper).post { mToast!!.cancel() }}}@SuppressLint("SoonBlockedPrivateApi")private fun hookToast(toast: Toast?) {try {if (!sIsHookFieldInit) {sField_TN = Toast::class.java.getDeclaredField(FIELD_NAME_TN)sField_TN?.run {isAccessible = truesField_TN_Handler = type.getDeclaredField(FIELD_NAME_HANDLER)}sField_TN_Handler?.isAccessible = truesIsHookFieldInit = true}val tn = sField_TN!![toast]val originHandler = sField_TN_Handler!![tn] as HandlersField_TN_Handler!![tn] = SafelyHandlerWrapper(originHandler)} catch (e: Exception) {Log.e(TAG, "Hook toast exception=$e")}}private class ToastRunnable(private val context: Context,private val text: CharSequence,private val duration: Int,private val isShowCenter: Boolean) : Runnable {@SuppressLint("ShowToast")override fun run() {if (mToast == null) {mToast = Toast.makeText(context, text, duration)} else {mToast!!.setText(text)if (isShowCenter) {mToast!!.setGravity(Gravity.CENTER, 0, 0)}mToast!!.duration = duration}hookToast(mToast)mToast!!.show()}}private class SafelyHandlerWrapper(private val originHandler: Handler?) : Handler() {override fun dispatchMessage(msg: Message) {try {super.dispatchMessage(msg)} catch (e: Exception) {Log.e(TAG, "Catch system toast exception:$e")}}override fun handleMessage(msg: Message) {originHandler?.handleMessage(msg)}}
}

10.FileManager:

package com.example.cameraxdemo.utils;import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;import androidx.annotation.NonNull;import com.example.cameraxdemo.app.CameraApp;import java.io.File;/*** @author: njb* @date: 2023/8/15 17:13* @desc:*/
public class FileManager {// 媒体模块根目录private static final String SAVE_MEDIA_ROOT_DIR = Environment.DIRECTORY_DCIM;// 媒体模块存储路径private static final String SAVE_MEDIA_DIR = SAVE_MEDIA_ROOT_DIR + "/CameraXApp";private static final String AVATAR_DIR = "/avatar";private static final String SAVE_MEDIA_VIDEO_DIR = SAVE_MEDIA_DIR + "/video";private static final String SAVE_MEDIA_PHOTO_DIR = SAVE_MEDIA_DIR + "/photo";// JPG后缀public static final String JPG_SUFFIX = ".jpg";// PNG后缀public static final String PNG_SUFFIX = ".png";// MP4后缀public static final String MP4_SUFFIX = ".mp4";// YUV后缀public static final String YUV_SUFFIX = ".yuv";// h264后缀public static final String H264_SUFFIX = ".h264";/*** 保存图片到系统相册** @param context* @param file*/public static String saveImage(Context context, File file) {ContentResolver localContentResolver = context.getContentResolver();ContentValues localContentValues = getImageContentValues(context, file, System.currentTimeMillis());localContentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, localContentValues);Intent localIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");final Uri localUri = Uri.fromFile(file);localIntent.setData(localUri);context.sendBroadcast(localIntent);return file.getAbsolutePath();}public static ContentValues getImageContentValues(Context paramContext, File paramFile, long paramLong) {ContentValues localContentValues = new ContentValues();localContentValues.put("title", paramFile.getName());localContentValues.put("_display_name", paramFile.getName());localContentValues.put("mime_type", "image/jpeg");localContentValues.put("datetaken", Long.valueOf(paramLong));localContentValues.put("date_modified", Long.valueOf(paramLong));localContentValues.put("date_added", Long.valueOf(paramLong));localContentValues.put("orientation", Integer.valueOf(0));localContentValues.put("_data", paramFile.getAbsolutePath());localContentValues.put("_size", Long.valueOf(paramFile.length()));return localContentValues;}/*** 获取App存储根目录*/public static String getAppRootDir() {String path = getStorageRootDir();FileUtil.createOrExistsDir(path);return path;}/*** 获取文件存储根目录*/public static String getStorageRootDir() {File filePath = CameraApp.Companion.getMInstance().getExternalFilesDir("");String path;if (filePath != null) {path = filePath.getAbsolutePath();} else {path = CameraApp.Companion.getMInstance().getFilesDir().getAbsolutePath();}return path;}/*** 图片地址*/public static String getCameraPhotoPath() {return getFolderDirPath(SAVE_MEDIA_PHOTO_DIR);}/*** 获取拍照普通图片文件*/public static File getSavedPictureFile(long timeStamp) {String fileName = "image"+ "_"+ + timeStamp + JPG_SUFFIX;return new File(getCameraPhotoPath(), fileName);}/*** 头像地址*/public static String getAvatarPath(String fileName) {String path;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {path = getFolderDirPath(SAVE_MEDIA_DIR + AVATAR_DIR);} else {path = getSaveDir(AVATAR_DIR);}return path + File.separator + fileName;}/*** 视频地址*/public static String getCameraVideoPath() {return getFolderDirPath(SAVE_MEDIA_VIDEO_DIR);}public static String getFolderDirPath(String dstDirPathToCreate) {File dstFileDir = new File(Environment.getExternalStorageDirectory(), dstDirPathToCreate);if (!dstFileDir.exists() && !dstFileDir.mkdirs()) {Log.e("Failed to create file", dstDirPathToCreate);return null;}return dstFileDir.getAbsolutePath();}/*** 获取具体模块存储目录*/public static String getSaveDir(@NonNull String directory) {String path = "";if (TextUtils.isEmpty(directory) || "/".equals(directory)) {path = "";} else if (directory.startsWith("/")) {path = directory;} else {path = "/" + directory;}path = getAppRootDir() + path;FileUtil.createOrExistsDir(path);return path;}/*** 通过媒体文件Uri获取文件-Android 11兼容** @param fileUri 文件Uri*/public static File getMediaUri2File(Uri fileUri) {String[] projection = {MediaStore.Images.Media.DATA};Cursor cursor = CameraApp.Companion.getMInstance().getContentResolver().query(fileUri, projection,null, null, null);if (cursor != null) {if (cursor.moveToFirst()) {int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);String path = cursor.getString(columnIndex);cursor.close();return new File(path);}}return null;}/*** 根据Uri获取图片绝对路径,解决Android4.4以上版本Uri转换** @param context  上下文* @param imageUri 图片地址*/public static String getImageAbsolutePath(Activity context, Uri imageUri) {if (context == null || imageUri == null)return null;if (DocumentsContract.isDocumentUri(context, imageUri)) {if (isExternalStorageDocument(imageUri)) {String docId = DocumentsContract.getDocumentId(imageUri);String[] split = docId.split(":");String type = split[0];if ("primary".equalsIgnoreCase(type)) {return Environment.getExternalStorageDirectory() + "/" + split[1];}} else if (isDownloadsDocument(imageUri)) {String id = DocumentsContract.getDocumentId(imageUri);Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.parseLong(id));return getDataColumn(context, contentUri, null, null);} else if (isMediaDocument(imageUri)) {String docId = DocumentsContract.getDocumentId(imageUri);String[] split = docId.split(":");String type = split[0];Uri contentUri = null;if ("image".equals(type)) {contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;} else if ("video".equals(type)) {contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;} else if ("audio".equals(type)) {contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;}String selection = MediaStore.Images.Media._ID + "=?";String[] selectionArgs = new String[]{split[1]};return getDataColumn(context, contentUri, selection, selectionArgs);}} // MediaStore (and general)else if ("content".equalsIgnoreCase(imageUri.getScheme())) {// Return the remote addressif (isGooglePhotosUri(imageUri))return imageUri.getLastPathSegment();return getDataColumn(context, imageUri, null, null);}// Fileelse if ("file".equalsIgnoreCase(imageUri.getScheme())) {return imageUri.getPath();}return null;}/*** @param uri The Uri to checkRemote.* @return Whether the Uri authority is ExternalStorageProvider.*/public static boolean isExternalStorageDocument(Uri uri) {return "com.android.externalstorage.documents".equals(uri.getAuthority());}/*** @param uri The Uri to checkRemote.* @return Whether the Uri authority is DownloadsProvider.*/public static boolean isDownloadsDocument(Uri uri) {return "com.android.providers.downloads.documents".equals(uri.getAuthority());}/*** @param uri The Uri to checkRemote.* @return Whether the Uri authority is MediaProvider.*/public static boolean isMediaDocument(Uri uri) {return "com.android.providers.media.documents".equals(uri.getAuthority());}/*** @param uri The Uri to checkRemote.* @return Whether the Uri authority is Google Photos.*/public static boolean isGooglePhotosUri(Uri uri) {return "com.google.android.apps.photos.content".equals(uri.getAuthority());}public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {String column = MediaStore.Images.Media.DATA;String[] projection = {column};try (Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null)) {if (cursor != null && cursor.moveToFirst()) {int index = cursor.getColumnIndexOrThrow(column);return cursor.getString(index);}}return null;}
}

11.FileUtil

package com.example.cameraxdemo.utils;
import android.app.Activity;
import android.app.Application;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;import androidx.core.content.FileProvider;import com.example.cameraxdemo.app.CameraApp;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;/*** @author: njb* @date: 2023/8/15 17:15* @desc:*/
public class FileUtil {private static final String LINE_SEP = System.getProperty("line.separator");private FileUtil() {throw new UnsupportedOperationException("u can't instantiate me...");}/*** Return the file by path.** @param filePath The path of file.* @return the file*/public static File getFileByPath(final String filePath) {return isSpace(filePath) ? null : new File(filePath);}/*** Return whether the file exists.** @param filePath The path of file.* @return {@code true}: yes<br>{@code false}: no*/public static boolean isFileExists(final String filePath) {return isFileExists(getFileByPath(filePath));}/*** Return whether the file exists.** @param file The file.* @return {@code true}: yes<br>{@code false}: no*/public static boolean isFileExists(final File file) {return file != null && file.exists();}/*** Rename the file.** @param filePath The path of file.* @param newName  The new name of file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean rename(final String filePath, final String newName) {return rename(getFileByPath(filePath), newName);}/*** Rename the file.** @param file    The file.* @param newName The new name of file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean rename(final File file, final String newName) {// file is null then return falseif (file == null) return false;// file doesn't exist then return falseif (!file.exists()) return false;// the new name is space then return falseif (isSpace(newName)) return false;// the new name equals old name then return trueif (newName.equals(file.getName())) return true;File newFile = new File(file.getParent() + File.separator + newName);// the new name of file exists then return falsereturn !newFile.exists()&& file.renameTo(newFile);}/*** Return whether it is a directory.** @param dirPath The path of directory.* @return {@code true}: yes<br>{@code false}: no*/public static boolean isDir(final String dirPath) {return isDir(getFileByPath(dirPath));}/*** Return whether it is a directory.** @param file The file.* @return {@code true}: yes<br>{@code false}: no*/public static boolean isDir(final File file) {return file != null && file.exists() && file.isDirectory();}/*** Return whether it is a file.** @param filePath The path of file.* @return {@code true}: yes<br>{@code false}: no*/public static boolean isFile(final String filePath) {return isFile(getFileByPath(filePath));}/*** Return whether it is a file.** @param file The file.* @return {@code true}: yes<br>{@code false}: no*/public static boolean isFile(final File file) {return file != null && file.exists() && file.isFile();}/*** Create a directory if it doesn't exist, otherwise do nothing.** @param dirPath The path of directory.* @return {@code true}: exists or creates successfully<br>{@code false}: otherwise*/public static boolean createOrExistsDir(final String dirPath) {return createOrExistsDir(getFileByPath(dirPath));}/*** Create a directory if it doesn't exist, otherwise do nothing.** @param file The file.* @return {@code true}: exists or creates successfully<br>{@code false}: otherwise*/public static boolean createOrExistsDir(final File file) {boolean isDirectory = file.isDirectory();return file != null && (file.exists() ? file.isDirectory() : file.mkdirs());}/*** Create a file if it doesn't exist, otherwise do nothing.** @param filePath The path of file.* @return {@code true}: exists or creates successfully<br>{@code false}: otherwise*/public static boolean createOrExistsFile(final String filePath) {return createOrExistsFile(getFileByPath(filePath));}/*** Create a file if it doesn't exist, otherwise do nothing.** @param file The file.* @return {@code true}: exists or creates successfully<br>{@code false}: otherwise*/public static boolean createOrExistsFile(final File file) {if (file == null) return false;if (file.exists()) return file.isFile();if (!createOrExistsDir(file.getParentFile())) return false;try {return file.createNewFile();} catch (IOException e) {e.printStackTrace();return false;}}/*** Create a file if it doesn't exist, otherwise delete old file before creating.** @param filePath The path of file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean createFileByDeleteOldFile(final String filePath) {return createFileByDeleteOldFile(getFileByPath(filePath));}/*** Create a file if it doesn't exist, otherwise delete old file before creating.** @param file The file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean createFileByDeleteOldFile(final File file) {if (file == null) return false;// file exists and unsuccessfully delete then return falseif (file.exists() && !file.delete()) return false;if (!createOrExistsDir(file.getParentFile())) return false;try {return file.createNewFile();} catch (IOException e) {e.printStackTrace();return false;}}/*** Copy the directory.** @param srcDirPath  The path of source directory.* @param destDirPath The path of destination directory.* @return {@code true}: success<br>{@code false}: fail*/public static boolean copyDir(final String srcDirPath,final String destDirPath) {return copyDir(getFileByPath(srcDirPath), getFileByPath(destDirPath));}/*** Copy the directory.** @param srcDirPath  The path of source directory.* @param destDirPath The path of destination directory.* @param listener    The replace listener.* @return {@code true}: success<br>{@code false}: fail*/public static boolean copyDir(final String srcDirPath,final String destDirPath,final OnReplaceListener listener) {return copyDir(getFileByPath(srcDirPath), getFileByPath(destDirPath), listener);}/*** Copy the directory.** @param srcDir  The source directory.* @param destDir The destination directory.* @return {@code true}: success<br>{@code false}: fail*/public static boolean copyDir(final File srcDir,final File destDir) {return copyOrMoveDir(srcDir, destDir, false);}/*** Copy the directory.** @param srcDir   The source directory.* @param destDir  The destination directory.* @param listener The replace listener.* @return {@code true}: success<br>{@code false}: fail*/public static boolean copyDir(final File srcDir,final File destDir,final OnReplaceListener listener) {return copyOrMoveDir(srcDir, destDir, listener, false);}/*** Copy the file.** @param srcFilePath  The path of source file.* @param destFilePath The path of destination file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean copyFile(final String srcFilePath,final String destFilePath) {return copyFile(getFileByPath(srcFilePath), getFileByPath(destFilePath));}/*** Copy the file.** @param srcFilePath  The path of source file.* @param destFilePath The path of destination file.* @param listener     The replace listener.* @return {@code true}: success<br>{@code false}: fail*/public static boolean copyFile(final String srcFilePath,final String destFilePath,final OnReplaceListener listener) {return copyFile(getFileByPath(srcFilePath), getFileByPath(destFilePath), listener);}/*** Copy the file.** @param srcFile  The source file.* @param destFile The destination file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean copyFile(final File srcFile,final File destFile) {return copyOrMoveFile(srcFile, destFile, false);}/*** Copy the file.** @param srcFile  The source file.* @param destFile The destination file.* @param listener The replace listener.* @return {@code true}: success<br>{@code false}: fail*/public static boolean copyFile(final File srcFile,final File destFile,final OnReplaceListener listener) {return copyOrMoveFile(srcFile, destFile, listener, false);}/*** Move the directory.** @param srcDirPath  The path of source directory.* @param destDirPath The path of destination directory.* @return {@code true}: success<br>{@code false}: fail*/public static boolean moveDir(final String srcDirPath,final String destDirPath) {return moveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath));}/*** Move the directory.** @param srcDirPath  The path of source directory.* @param destDirPath The path of destination directory.* @param listener    The replace listener.* @return {@code true}: success<br>{@code false}: fail*/public static boolean moveDir(final String srcDirPath,final String destDirPath,final OnReplaceListener listener) {return moveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath), listener);}/*** Move the directory.** @param srcDir  The source directory.* @param destDir The destination directory.* @return {@code true}: success<br>{@code false}: fail*/public static boolean moveDir(final File srcDir,final File destDir) {return copyOrMoveDir(srcDir, destDir, true);}/*** Move the directory.** @param srcDir   The source directory.* @param destDir  The destination directory.* @param listener The replace listener.* @return {@code true}: success<br>{@code false}: fail*/public static boolean moveDir(final File srcDir,final File destDir,final OnReplaceListener listener) {return copyOrMoveDir(srcDir, destDir, listener, true);}/*** Move the file.** @param srcFilePath  The path of source file.* @param destFilePath The path of destination file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean moveFile(final String srcFilePath,final String destFilePath) {return moveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath));}/*** Move the file.** @param srcFilePath  The path of source file.* @param destFilePath The path of destination file.* @param listener     The replace listener.* @return {@code true}: success<br>{@code false}: fail*/public static boolean moveFile(final String srcFilePath,final String destFilePath,final OnReplaceListener listener) {return moveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath), listener);}/*** Move the file.** @param srcFile  The source file.* @param destFile The destination file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean moveFile(final File srcFile,final File destFile) {return copyOrMoveFile(srcFile, destFile, true);}/*** Move the file.** @param srcFile  The source file.* @param destFile The destination file.* @param listener The replace listener.* @return {@code true}: success<br>{@code false}: fail*/public static boolean moveFile(final File srcFile,final File destFile,final OnReplaceListener listener) {return copyOrMoveFile(srcFile, destFile, listener, true);}private static boolean copyOrMoveDir(final File srcDir,final File destDir,final boolean isMove) {return copyOrMoveDir(srcDir, destDir, new OnReplaceListener() {@Overridepublic boolean onReplace() {return true;}}, isMove);}private static boolean copyOrMoveDir(final File srcDir,final File destDir,final OnReplaceListener listener,final boolean isMove) {if (srcDir == null || destDir == null) return false;// destDir's path locate in srcDir's path then return falseString srcPath = srcDir.getPath() + File.separator;String destPath = destDir.getPath() + File.separator;if (destPath.contains(srcPath)) return false;if (!srcDir.exists() || !srcDir.isDirectory()) return false;if (destDir.exists()) {if (listener == null || listener.onReplace()) {// require delete the old directoryif (!deleteAllInDir(destDir)) {// unsuccessfully delete then return falsereturn false;}} else {return true;}}if (!createOrExistsDir(destDir)) return false;File[] files = srcDir.listFiles();for (File file : files) {File oneDestFile = new File(destPath + file.getName());if (file.isFile()) {if (!copyOrMoveFile(file, oneDestFile, listener, isMove)) return false;} else if (file.isDirectory()) {if (!copyOrMoveDir(file, oneDestFile, listener, isMove)) return false;}}return !isMove || deleteDir(srcDir);}private static boolean copyOrMoveFile(final File srcFile,final File destFile,final boolean isMove) {return copyOrMoveFile(srcFile, destFile, new OnReplaceListener() {@Overridepublic boolean onReplace() {return true;}}, isMove);}private static boolean copyOrMoveFile(final File srcFile,final File destFile,final OnReplaceListener listener,final boolean isMove) {if (srcFile == null || destFile == null) return false;// srcFile equals destFile then return falseif (srcFile.equals(destFile)) return false;// srcFile doesn't exist or isn't a file then return falseif (!srcFile.exists() || !srcFile.isFile()) return false;if (destFile.exists()) {if (listener == null || listener.onReplace()) {// require delete the old fileif (!destFile.delete()) {// unsuccessfully delete then return falsereturn false;}} else {return true;}}if (!createOrExistsDir(destFile.getParentFile())) return false;try {return writeFileFromIS(destFile, new FileInputStream(srcFile))&& !(isMove && !deleteFile(srcFile));} catch (FileNotFoundException e) {e.printStackTrace();return false;}}/*** Delete the directory.** @param filePath The path of file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean delete(final String filePath) {return delete(getFileByPath(filePath));}/*** Delete the directory.** @param file The file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean delete(final File file) {if (file == null) return false;if (file.isDirectory()) {return deleteDir(file);}return deleteFile(file);}/*** Delete the directory.** @param dirPath The path of directory.* @return {@code true}: success<br>{@code false}: fail*/public static boolean deleteDir(final String dirPath) {return deleteDir(getFileByPath(dirPath));}/*** Delete the directory.** @param dir The directory.* @return {@code true}: success<br>{@code false}: fail*/public static boolean deleteDir(final File dir) {if (dir == null) return false;// dir doesn't exist then return trueif (!dir.exists()) return true;// dir isn't a directory then return falseif (!dir.isDirectory()) return false;File[] files = dir.listFiles();if (files != null && files.length != 0) {for (File file : files) {if (file.isFile()) {if (!file.delete()) return false;} else if (file.isDirectory()) {if (!deleteDir(file)) return false;}}}return dir.delete();}public static boolean createDir(String path) {File file = new File(path);if (!file.exists()) {if (!file.mkdirs()) {//  FL.d(TAG, "创建失败,请检查路径和是否配置文件权限!");return false;}return true;}return false;}/*** Delete the file.** @param srcFilePath The path of source file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean deleteFile(final String srcFilePath) {return deleteFile(getFileByPath(srcFilePath));}/*** Delete the file.** @param file The file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean deleteFile(final File file) {return file != null && (!file.exists() || file.isFile() && file.delete());}/*** Delete the all in directory.** @param dirPath The path of directory.* @return {@code true}: success<br>{@code false}: fail*/public static boolean deleteAllInDir(final String dirPath) {return deleteAllInDir(getFileByPath(dirPath));}/*** Delete the all in directory.** @param dir The directory.* @return {@code true}: success<br>{@code false}: fail*/public static boolean deleteAllInDir(final File dir) {return deleteFilesInDirWithFilter(dir, new FileFilter() {@Overridepublic boolean accept(File pathname) {return true;}});}/*** Delete all files in directory.** @param dirPath The path of directory.* @return {@code true}: success<br>{@code false}: fail*/public static boolean deleteFilesInDir(final String dirPath) {return deleteFilesInDir(getFileByPath(dirPath));}/*** Delete all files in directory.** @param dir The directory.* @return {@code true}: success<br>{@code false}: fail*/public static boolean deleteFilesInDir(final File dir) {return deleteFilesInDirWithFilter(dir, new FileFilter() {@Overridepublic boolean accept(File pathname) {return pathname.isFile();}});}/*** Delete all files that satisfy the filter in directory.** @param dirPath The path of directory.* @param filter  The filter.* @return {@code true}: success<br>{@code false}: fail*/public static boolean deleteFilesInDirWithFilter(final String dirPath,final FileFilter filter) {return deleteFilesInDirWithFilter(getFileByPath(dirPath), filter);}/*** Delete all files that satisfy the filter in directory.** @param dir    The directory.* @param filter The filter.* @return {@code true}: success<br>{@code false}: fail*/public static boolean deleteFilesInDirWithFilter(final File dir, final FileFilter filter) {if (dir == null) return false;// dir doesn't exist then return trueif (!dir.exists()) return true;// dir isn't a directory then return falseif (!dir.isDirectory()) return false;File[] files = dir.listFiles();if (files != null && files.length != 0) {for (File file : files) {if (filter.accept(file)) {if (file.isFile()) {if (!file.delete()) return false;} else if (file.isDirectory()) {if (!deleteDir(file)) return false;}}}}return true;}/*** Return the files in directory.* <p>Doesn't traverse subdirectories</p>** @param dirPath The path of directory.* @return the files in directory*/public static List<File> listFilesInDir(final String dirPath) {return listFilesInDir(dirPath, false);}/*** Return the files in directory.* <p>Doesn't traverse subdirectories</p>** @param dir The directory.* @return the files in directory*/public static List<File> listFilesInDir(final File dir) {return listFilesInDir(dir, false);}/*** Return the files in directory.** @param dirPath     The path of directory.* @param isRecursive True to traverse subdirectories, false otherwise.* @return the files in directory*/public static List<File> listFilesInDir(final String dirPath, final boolean isRecursive) {return listFilesInDir(getFileByPath(dirPath), isRecursive);}/*** Return the files in directory.** @param dir         The directory.* @param isRecursive True to traverse subdirectories, false otherwise.* @return the files in directory*/public static List<File> listFilesInDir(final File dir, final boolean isRecursive) {return listFilesInDirWithFilter(dir, new FileFilter() {@Overridepublic boolean accept(File pathname) {return true;}}, isRecursive);}/*** Return the files that satisfy the filter in directory.* <p>Doesn't traverse subdirectories</p>** @param dirPath The path of directory.* @param filter  The filter.* @return the files that satisfy the filter in directory*/public static List<File> listFilesInDirWithFilter(final String dirPath,final FileFilter filter) {return listFilesInDirWithFilter(getFileByPath(dirPath), filter, false);}/*** Return the files that satisfy the filter in directory.* <p>Doesn't traverse subdirectories</p>** @param dir    The directory.* @param filter The filter.* @return the files that satisfy the filter in directory*/public static List<File> listFilesInDirWithFilter(final File dir,final FileFilter filter) {return listFilesInDirWithFilter(dir, filter, false);}/*** Return the files that satisfy the filter in directory.** @param dirPath     The path of directory.* @param filter      The filter.* @param isRecursive True to traverse subdirectories, false otherwise.* @return the files that satisfy the filter in directory*/public static List<File> listFilesInDirWithFilter(final String dirPath,final FileFilter filter,final boolean isRecursive) {return listFilesInDirWithFilter(getFileByPath(dirPath), filter, isRecursive);}/*** Return the files that satisfy the filter in directory.** @param dir         The directory.* @param filter      The filter.* @param isRecursive True to traverse subdirectories, false otherwise.* @return the files that satisfy the filter in directory*/public static List<File> listFilesInDirWithFilter(final File dir,final FileFilter filter,final boolean isRecursive) {if (!isDir(dir)) return null;List<File> list = new ArrayList<>();File[] files = dir.listFiles();if (files != null && files.length != 0) {for (File file : files) {if (filter.accept(file)) {list.add(file);}if (isRecursive && file.isDirectory()) {//noinspection ConstantConditionslist.addAll(listFilesInDirWithFilter(file, filter, true));}}}return list;}/*** Return the time that the file was last modified.** @param filePath The path of file.* @return the time that the file was last modified*/public static long getFileLastModified(final String filePath) {return getFileLastModified(getFileByPath(filePath));}/*** Return the time that the file was last modified.** @param file The file.* @return the time that the file was last modified*/public static long getFileLastModified(final File file) {if (file == null) return -1;return file.lastModified();}/*** Return the charset of file simply.** @param filePath The path of file.* @return the charset of file simply*/public static String getFileCharsetSimple(final String filePath) {return getFileCharsetSimple(getFileByPath(filePath));}/*** Return the charset of file simply.** @param file The file.* @return the charset of file simply*/public static String getFileCharsetSimple(final File file) {int p = 0;InputStream is = null;try {is = new BufferedInputStream(new FileInputStream(file));p = (is.read() << 8) + is.read();} catch (IOException e) {e.printStackTrace();} finally {try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}}switch (p) {case 0xefbb:return "UTF-8";case 0xfffe:return "Unicode";case 0xfeff:return "UTF-16BE";default:return "GBK";}}/*** Return the number of lines of file.** @param filePath The path of file.* @return the number of lines of file*/public static int getFileLines(final String filePath) {return getFileLines(getFileByPath(filePath));}/*** Return the number of lines of file.** @param file The file.* @return the number of lines of file*/public static int getFileLines(final File file) {int count = 1;InputStream is = null;try {is = new BufferedInputStream(new FileInputStream(file));byte[] buffer = new byte[1024];int readChars;if (LINE_SEP.endsWith("\n")) {while ((readChars = is.read(buffer, 0, 1024)) != -1) {for (int i = 0; i < readChars; ++i) {if (buffer[i] == '\n') ++count;}}} else {while ((readChars = is.read(buffer, 0, 1024)) != -1) {for (int i = 0; i < readChars; ++i) {if (buffer[i] == '\r') ++count;}}}} catch (IOException e) {e.printStackTrace();} finally {try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}}return count;}/*** Return the size of directory.** @param dirPath The path of directory.* @return the size of directory*/public static String getDirSize(final String dirPath) {return getDirSize(getFileByPath(dirPath));}/*** Return the size of directory.** @param dir The directory.* @return the size of directory*/public static String getDirSize(final File dir) {long len = getDirLength(dir);return len == -1 ? "" : byte2FitMemorySize(len);}/*** Return the length of file.** @param filePath The path of file.* @return the length of file*/public static String getFileSize(final String filePath) {long len = getFileLength(filePath);return len == -1 ? "" : byte2FitMemorySize(len);}/*** Return the length of file.** @param file The file.* @return the length of file*/public static String getFileSize(final File file) {long len = getFileLength(file);return len == -1 ? "" : byte2FitMemorySize(len);}/*** Return the length of directory.** @param dirPath The path of directory.* @return the length of directory*/public static long getDirLength(final String dirPath) {return getDirLength(getFileByPath(dirPath));}/*** Return the length of directory.** @param dir The directory.* @return the length of directory*/public static long getDirLength(final File dir) {if (!isDir(dir)) return -1;long len = 0;File[] files = dir.listFiles();if (files != null && files.length != 0) {for (File file : files) {if (file.isDirectory()) {len += getDirLength(file);} else {len += file.length();}}}return len;}/*** Return the length of file.** @param filePath The path of file.* @return the length of file*/public static long getFileLength(final String filePath) {boolean isURL = filePath.matches("[a-zA-z]+://[^\\s]*");if (isURL) {try {HttpURLConnection conn = (HttpURLConnection) new URL(filePath).openConnection();conn.setRequestProperty("Accept-Encoding", "identity");conn.connect();if (conn.getResponseCode() == 200) {return conn.getContentLength();}return -1;} catch (IOException e) {e.printStackTrace();}}return getFileLength(getFileByPath(filePath));}/*** Return the length of file.** @param file The file.* @return the length of file*/public static long getFileLength(final File file) {if (!isFile(file)) return -1;return file.length();}/*** Return the MD5 of file.** @param filePath The path of file.* @return the md5 of file*/public static String getFileMD5ToString(final String filePath) {File file = isSpace(filePath) ? null : new File(filePath);return getFileMD5ToString(file);}/*** Return the MD5 of file.** @param file The file.* @return the md5 of file*/public static String getFileMD5ToString(final File file) {return bytes2HexString(getFileMD5(file));}/*** Return the MD5 of file.** @param filePath The path of file.* @return the md5 of file*/public static byte[] getFileMD5(final String filePath) {return getFileMD5(getFileByPath(filePath));}/*** Return the MD5 of file.** @param file The file.* @return the md5 of file*/public static byte[] getFileMD5(final File file) {if (file == null) return null;DigestInputStream dis = null;try {FileInputStream fis = new FileInputStream(file);MessageDigest md = MessageDigest.getInstance("MD5");dis = new DigestInputStream(fis, md);byte[] buffer = new byte[1024 * 256];while (true) {if (!(dis.read(buffer) > 0)) break;}md = dis.getMessageDigest();return md.digest();} catch (NoSuchAlgorithmException | IOException e) {e.printStackTrace();} finally {try {if (dis != null) {dis.close();}} catch (IOException e) {e.printStackTrace();}}return null;}/*** Return the file's path of directory.** @param file The file.* @return the file's path of directory*/public static String getDirName(final File file) {if (file == null) return "";return getDirName(file.getAbsolutePath());}/*** Return the file's path of directory.** @param filePath The path of file.* @return the file's path of directory*/public static String getDirName(final String filePath) {if (isSpace(filePath)) return "";int lastSep = filePath.lastIndexOf(File.separator);return lastSep == -1 ? "" : filePath.substring(0, lastSep + 1);}/*** Return the name of file.** @param file The file.* @return the name of file*/public static String getFileName(final File file) {if (file == null) return "";return getFileName(file.getAbsolutePath());}/*** Return the name of file.** @param filePath The path of file.* @return the name of file*/public static String getFileName(final String filePath) {if (isSpace(filePath)) return "";int lastSep = filePath.lastIndexOf(File.separator);return lastSep == -1 ? filePath : filePath.substring(lastSep + 1);}/*** Return the name of file without extension.** @param file The file.* @return the name of file without extension*/public static String getFileNameNoExtension(final File file) {if (file == null) return "";return getFileNameNoExtension(file.getPath());}/*** Return the name of file without extension.** @param filePath The path of file.* @return the name of file without extension*/public static String getFileNameNoExtension(final String filePath) {if (isSpace(filePath)) return "";int lastPoi = filePath.lastIndexOf('.');int lastSep = filePath.lastIndexOf(File.separator);if (lastSep == -1) {return (lastPoi == -1 ? filePath : filePath.substring(0, lastPoi));}if (lastPoi == -1 || lastSep > lastPoi) {return filePath.substring(lastSep + 1);}return filePath.substring(lastSep + 1, lastPoi);}/*** Return the extension of file.** @param file The file.* @return the extension of file*/public static String getFileExtension(final File file) {if (file == null) return "";return getFileExtension(file.getPath());}/*** Return the extension of file.** @param filePath The path of file.* @return the extension of file*/public static String getFileExtension(final String filePath) {if (isSpace(filePath)) return "";int lastPoi = filePath.lastIndexOf('.');int lastSep = filePath.lastIndexOf(File.separator);if (lastPoi == -1 || lastSep >= lastPoi) return "";return filePath.substring(lastPoi + 1);}public interface OnReplaceListener {boolean onReplace();}private static final char HEX_DIGITS[] ={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};private static String bytes2HexString(final byte[] bytes) {if (bytes == null) return "";int len = bytes.length;if (len <= 0) return "";char[] ret = new char[len << 1];for (int i = 0, j = 0; i < len; i++) {ret[j++] = HEX_DIGITS[bytes[i] >> 4 & 0x0f];ret[j++] = HEX_DIGITS[bytes[i] & 0x0f];}return new String(ret);}private static String byte2FitMemorySize(final long byteNum) {if (byteNum < 0) {return "shouldn't be less than zero!";} else if (byteNum < 1024) {return String.format(Locale.getDefault(), "%.1fB", (double) byteNum);} else if (byteNum < 1048576) {return String.format(Locale.getDefault(), "%.1fKB", (double) byteNum / 1024);} else if (byteNum < 1073741824) {return String.format(Locale.getDefault(), "%.1fMB", (double) byteNum / 1048576);} else {return String.format(Locale.getDefault(), "%.1fGB", (double) byteNum / 1073741824);}}private static boolean isSpace(final String s) {if (s == null) return true;for (int i = 0, len = s.length(); i < len; ++i) {if (!Character.isWhitespace(s.charAt(i))) {return false;}}return true;}private static boolean writeFileFromIS(final File file,final InputStream is) {OutputStream os = null;try {os = new BufferedOutputStream(new FileOutputStream(file));byte data[] = new byte[8192];int len;while ((len = is.read(data, 0, 8192)) != -1) {os.write(data, 0, len);}return true;} catch (IOException e) {e.printStackTrace();return false;} finally {try {is.close();} catch (IOException e) {e.printStackTrace();}try {if (os != null) {os.close();}} catch (IOException e) {e.printStackTrace();}}}/*** 写入文件*/public static void writeFrame(String fileName, byte[] data) {try {BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));bos.write(data);bos.flush();bos.close();// Log.e(TAG, "" + data.length + " bytes have been written to " + filesDir + fileName + ".jpg");} catch (IOException e) {e.printStackTrace();}}/*** 拷贝文件* 使用文件通道,提高文件复制效率** @param fromFile 来源文件地址* @param toFile   复制后文件地址* @return 是否复制成功*/public static boolean copyFileWithChannel(String fromFile, String toFile) {FileInputStream fsIn = null;FileOutputStream fsOut = null;FileChannel fcIn = null;FileChannel fcOut = null;try {fsIn = new FileInputStream(fromFile);fsOut = new FileOutputStream(toFile);// 得到对应文件通道fcIn = fsIn.getChannel();fcOut = fsOut.getChannel();// 连接两个通道,并且从in通道读取,然后写入out通道fcIn.transferTo(0, fcIn.size(), fcOut);return true;} catch (Exception ex) {ex.printStackTrace();return false;} finally {try {if (fsIn != null) {fsIn.close();}if (fsOut != null) {fsOut.close();}if (fcIn != null) {fcIn.close();}if (fcOut != null) {fcOut.close();}} catch (IOException e) {e.printStackTrace();}}}public static void saveRgbaDataToFile(File file, byte[] rgba, int width, int height) {Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);ByteBuffer buf = ByteBuffer.wrap(rgba);bitmap.copyPixelsFromBuffer(buf);try {FileOutputStream fos = new FileOutputStream(file);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (IOException e) {e.printStackTrace();}}public static void saveRgbaDataToFile(File file, ByteBuffer rgba, int width, int height) {Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);rgba.rewind();bitmap.copyPixelsFromBuffer(rgba);try {FileOutputStream fos = new FileOutputStream(file);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** Copy the file from assets.** @param assetsFilePath The path of file in assets.* @param destFilePath   The path of destination file.* @return {@code true}: success<br>{@code false}: fail*/public static boolean copyFileFromAssets(final String assetsFilePath, final String destFilePath) {boolean res = true;try {String[] assets = CameraApp.Companion.getMInstance().getAssets().list(assetsFilePath);if (assets.length > 0) {for (String asset : assets) {res &= copyFileFromAssets(assetsFilePath + "/" + asset, destFilePath + "/" + asset);}} else {res = writeFileFromIS(new File(destFilePath),CameraApp.Companion.getMInstance().getAssets().open(assetsFilePath));}} catch (IOException e) {e.printStackTrace();res = false;}return res;}/*** 获取jpg图片输出路径** @return 输出路径*/public static Object getHeadJpgFile() {String fileName = System.currentTimeMillis() + FileManager.JPG_SUFFIX;String headPath = FileManager.getAvatarPath(fileName);File imgFile;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {imgFile = new File(headPath);// 通过 MediaStore API 插入file 为了拿到系统裁剪要保存到的uri(因为App没有权限不能访问公共存储空间,需要通过 MediaStore API来操作)ContentValues values = new ContentValues();values.put(MediaStore.Images.Media.DATA, imgFile.getAbsolutePath());values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");return CameraApp.Companion.getMInstance().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);} else {imgFile = new File(headPath);}return imgFile;}public static void saveBitmap(Bitmap bitmap, String path) {File filePic;try {filePic = new File(path);if (!filePic.exists()) {filePic.getParentFile().mkdirs();filePic.createNewFile();}FileOutputStream fos = new FileOutputStream(filePic);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (IOException e) {Log.e("tag", "saveBitmap: " + e.getMessage());return;}Log.i("tag", "saveBitmap success: " + filePic.getAbsolutePath());}/*** 从File获取Uri* 此方式获取视频Uri原生视频分享报导入失败,解决方式参考 getVideoUri()** @param file 文件* @return uri*/public static Uri getUriFromFile(File file, Application application) {Uri mUri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {Log.w("lzq", "name:" + application.getPackageName());mUri = FileProvider.getUriForFile(application,application.getPackageName() + ".fileprovider", file);} else {mUri = Uri.fromFile(file);}return mUri;}/*** 从文件路径获取uri** @param path 文件路径* @return uri*/public static Uri getUriFromPath(String path, Application application) {File file = new File(path);if (file.exists()) {return getUriFromFile(file, application);} else {Log.e("File is not exist","");return null;}}/*** 保存rgba数据为jpg文件** @param bytes  rgba数据* @param width  宽度* @param height 高度* @param file   保存文件*/public static void saveRgbaDataToFile(byte[] bytes, int width, int height, File file) {Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);ByteBuffer buf = ByteBuffer.wrap(bytes);bitmap.copyPixelsFromBuffer(buf);try {FileOutputStream fos = new FileOutputStream(file);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static File createFile(String path) {File file = new File(path);if (!file.getParentFile().exists()) {// FL.d(TAG, "目标文件所在路径不存在,准备创建……");if (!createDir(file.getParent())) {// FL.d(TAG, "创建目录文件所在的目录失败!文件路径【" + path + "】");}}// 创建目标文件try {if (!file.exists()) {if (file.createNewFile()) {//  FL.d(TAG, "创建文件成功:" + file.getAbsolutePath());}return file;} else {return file;}} catch (IOException e) {e.printStackTrace();}return null;}/*** 图片压缩并且存储** @param targetFile   保存目标文件* @param originalPath 图片地址* @param scale        图片缩放值*/public static void compressAndSaveImage(File targetFile, String originalPath, int scale) {Bitmap bitmap = null;BufferedInputStream bufferedInputStream = null;FileOutputStream fileOutputStream = null;try {// 1、得到图片的宽、高BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;bufferedInputStream = new BufferedInputStream(new FileInputStream(originalPath));bitmap = BitmapFactory.decodeStream(bufferedInputStream, null, options);if (bitmap != null) {bitmap.recycle();}bufferedInputStream.close();int originalImageWidth = options.outWidth;int originalImageHeight = options.outHeight;// 2、获取图片方向int orientation = getImageOrientation(originalPath);int rotate = 0;switch (orientation) { // 判断是否需要旋转case ExifInterface.ORIENTATION_ROTATE_270:rotate = -90;break;case ExifInterface.ORIENTATION_ROTATE_180:rotate = 180;break;case ExifInterface.ORIENTATION_ROTATE_90:rotate = 90;break;}// 3、计算图片压缩inSampleSizeint maxValue = Math.max(originalImageWidth, originalImageHeight);if (maxValue > 3000) {options.inSampleSize = scale * 3;} else if (maxValue > 2000) {options.inSampleSize = scale * 2;} else if (maxValue > 1500) {options.inSampleSize = (int) (scale * 1.5);} else if (maxValue > 1000) {options.inSampleSize = (int) (scale * 1.1);} else {options.inSampleSize = scale;}options.inJustDecodeBounds = false;// 4、图片方向纠正和压缩(生成缩略图)bufferedInputStream = new BufferedInputStream(new FileInputStream(originalPath));bitmap = BitmapFactory.decodeStream(bufferedInputStream, null, options);bufferedInputStream.close();if (bitmap == null) {return;}targetFile.getParentFile().mkdirs();fileOutputStream = new FileOutputStream(targetFile);if (rotate != 0) {Matrix matrix = new Matrix();matrix.setRotate(rotate);Bitmap bitmapOld = bitmap;bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, false);bitmapOld.recycle();}// 5、保存图片bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fileOutputStream);} catch (Exception e) {e.printStackTrace();} finally {try {if (bufferedInputStream != null) {bufferedInputStream.close();}if (fileOutputStream != null) {fileOutputStream.flush();}if (fileOutputStream != null) {fileOutputStream.close();}} catch (IOException io) {io.printStackTrace();}if (bitmap != null && bitmap.isRecycled()) {bitmap.recycle();}}}/*** 获取一张图片在手机上的方向值*/public static int getImageOrientation(String uri) throws IOException {if (!new File(uri).exists()) {return 0;}return new ExifInterface(uri).getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);}/*** 根据Uri获取图片绝对路径,解决Android4.4以上版本Uri转换** @param context  上下文* @param imageUri 图片地址*/public static String getImageAbsolutePath(Activity context, Uri imageUri) {if (context == null || imageUri == null)return null;if (DocumentsContract.isDocumentUri(context, imageUri)) {if (isExternalStorageDocument(imageUri)) {String docId = DocumentsContract.getDocumentId(imageUri);String[] split = docId.split(":");String type = split[0];if ("primary".equalsIgnoreCase(type)) {return Environment.getExternalStorageDirectory() + "/" + split[1];}} else if (isDownloadsDocument(imageUri)) {String id = DocumentsContract.getDocumentId(imageUri);Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.parseLong(id));return getDataColumn(context, contentUri, null, null);} else if (isMediaDocument(imageUri)) {String docId = DocumentsContract.getDocumentId(imageUri);String[] split = docId.split(":");String type = split[0];Uri contentUri = null;if ("image".equals(type)) {contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;} else if ("video".equals(type)) {contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;} else if ("audio".equals(type)) {contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;}String selection = MediaStore.Images.Media._ID + "=?";String[] selectionArgs = new String[]{split[1]};return getDataColumn(context, contentUri, selection, selectionArgs);}} // MediaStore (and general)else if ("content".equalsIgnoreCase(imageUri.getScheme())) {// Return the remote addressif (isGooglePhotosUri(imageUri))return imageUri.getLastPathSegment();return getDataColumn(context, imageUri, null, null);}// Fileelse if ("file".equalsIgnoreCase(imageUri.getScheme())) {return imageUri.getPath();}return null;}/*** @param uri The Uri to checkRemote.* @return Whether the Uri authority is ExternalStorageProvider.*/public static boolean isExternalStorageDocument(Uri uri) {return "com.android.externalstorage.documents".equals(uri.getAuthority());}/*** @param uri The Uri to checkRemote.* @return Whether the Uri authority is DownloadsProvider.*/public static boolean isDownloadsDocument(Uri uri) {return "com.android.providers.downloads.documents".equals(uri.getAuthority());}/*** @param uri The Uri to checkRemote.* @return Whether the Uri authority is MediaProvider.*/public static boolean isMediaDocument(Uri uri) {return "com.android.providers.media.documents".equals(uri.getAuthority());}/*** @param uri The Uri to checkRemote.* @return Whether the Uri authority is Google Photos.*/public static boolean isGooglePhotosUri(Uri uri) {return "com.google.android.apps.photos.content".equals(uri.getAuthority());}public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {String column = MediaStore.Images.Media.DATA;String[] projection = {column};try (Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null)) {if (cursor != null && cursor.moveToFirst()) {int index = cursor.getColumnIndexOrThrow(column);return cursor.getString(index);}}return null;}
}

12.VideoFileUtils

package com.example.cameraxdemo.utilsimport android.content.Context
import android.os.Environment
import com.example.cameraxdemo.R
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale/***@author: njb*@date:   2023/8/15 17:13*@desc:*/
object VideoFileUtils {/*** 获取视频文件路径*/fun getVideoName(): String {val videoPath = Environment.getExternalStorageDirectory().toString() + "/CameraXApp"val dir = File(videoPath)if (!dir.exists() && !dir.mkdirs()) {ToastUtils.shortToast("文件不存在")}return videoPath}/*** 获取图片文件路径*/fun getImageFileName(): String {val imagePath = Environment.getExternalStorageDirectory().toString() + "/images"val dir = File(imagePath)if (!dir.exists() && !dir.mkdirs()) {ToastUtils.shortToast("文件不存在")}return imagePath}/*** 拍照文件保存路径* @param context* @return*/fun getPhotoDir(context: Context?): String? {return FileManager.getFolderDirPath("DCIM/Camera/CameraXApp/photo")}/*** 视频文件保存路径* @param context* @return*/fun getVideoDir(): String? {return FileManager.getFolderDirPath("DCIM/Camera/CameraXApp/video")}/** Use external media if it is available, our app's file directory otherwise */fun getOutputDirectory(context: Context): File {val appContext = context.applicationContextval mediaDir = context.externalMediaDirs.firstOrNull()?.let {File(it, appContext.resources.getString(R.string.app_name)).apply { mkdirs() }}return if (mediaDir != null && mediaDir.exists())mediaDir else appContext.filesDir}fun createFile(baseFolder: File, format: String, extension: String) =File(baseFolder, SimpleDateFormat(format, Locale.US).format(System.currentTimeMillis()) + extension)
}

13.CameraApp:

package com.example.cameraxdemo.appimport android.app.Application/***@author: njb*@date:   2023/8/15 17:07*@desc:*/
class CameraApp : Application() {override fun onCreate() {super.onCreate()mInstance = this}companion object {lateinit var mInstance: CameraAppprivate set}
}

14.完整的示例代码如下:

package com.example.cameraxdemoimport android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.webkit.MimeTypeMap
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.AspectRatio
import androidx.camera.core.Camera
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.core.VideoCapture.OnVideoSavedCallback
import androidx.camera.core.VideoCapture.OutputFileOptions
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.view.PreviewView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.net.toFile
import com.blankj.utilcode.util.LogUtils
import com.example.cameraxdemo.activity.CameraActivity
import com.example.cameraxdemo.utils.Constants
import com.example.cameraxdemo.utils.Constants.Companion.DATE_FORMAT
import com.example.cameraxdemo.utils.Constants.Companion.PHOTO_EXTENSION
import com.example.cameraxdemo.utils.Constants.Companion.REQUIRED_PERMISSIONS
import com.example.cameraxdemo.utils.FileManager
import com.example.cameraxdemo.utils.ToastUtils
import com.example.cameraxdemo.utils.VideoFileUtils.createFile
import com.example.cameraxdemo.utils.VideoFileUtils.getOutputDirectory
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executorsclass MainActivity : AppCompatActivity() {private var imageCamera: ImageCapture? = nullprivate lateinit var cameraExecutor: ExecutorServiceprivate var videoCapture: VideoCapture? = nullprivate var cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA//当前相机private var preview: Preview? = null//预览对象private var cameraProvider: ProcessCameraProvider? = null//相机信息private lateinit var camera: Camera //相机对象private var isRecordVideo: Boolean = falseprivate val TAG = "CameraXApp"private lateinit var outputDirectory: Fileprivate var lensFacing: Int = CameraSelector.LENS_FACING_BACKprivate val btnCameraCapture: Button by lazy { findViewById(R.id.btnCameraCapture) }private val btnVideo: Button by lazy { findViewById(R.id.btnVideo) }private val btnSwitch: Button by lazy { findViewById(R.id.btnSwitch) }private val btnOpenCamera: Button by lazy { findViewById(R.id.btnOpenCamera) }private val viewFinder: PreviewView by lazy { findViewById(R.id.mPreviewView) }override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)initPermission()initView()}private fun initView() {outputDirectory = getOutputDirectory(this)}@SuppressLint("RestrictedApi")private fun initListener() {btnCameraCapture.setOnClickListener {takePhoto()}btnVideo.setOnClickListener {if (!isRecordVideo) {takeVideo()isRecordVideo = truebtnVideo.text = "停止录像"} else {isRecordVideo = falsevideoCapture?.stopRecording()//停止录制//preview?.clear()//清除预览btnVideo.text = "开始录像"}}btnSwitch.setOnClickListener {cameraSelector = if (cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA) {CameraSelector.DEFAULT_FRONT_CAMERA} else {CameraSelector.DEFAULT_BACK_CAMERA}if (!isRecordVideo) {startCamera()}}btnOpenCamera.setOnClickListener {val intent = Intent(this, CameraActivity::class.java)startActivity(intent)}}private fun initPermission() {if (checkPermissions()) {// ImageCapturestartCamera()} else {requestPermission()}}private fun requestPermission() {when {Build.VERSION.SDK_INT >= 33 -> {ActivityCompat.requestPermissions(this,arrayOf(Manifest.permission.READ_MEDIA_IMAGES,Manifest.permission.READ_MEDIA_AUDIO,Manifest.permission.READ_MEDIA_VIDEO,Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO),Constants.REQUEST_CODE_PERMISSIONS)}else -> {ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, Constants.REQUEST_CODE_PERMISSIONS)}}}/*** 开始拍照*/private fun takePhoto() {val imageCapture = imageCamera ?: returnval photoFile = createFile(outputDirectory, DATE_FORMAT, PHOTO_EXTENSION)val metadata = ImageCapture.Metadata().apply {// Mirror image when using the front cameraisReversedHorizontal = lensFacing == CameraSelector.LENS_FACING_FRONT}val outputOptions =ImageCapture.OutputFileOptions.Builder(photoFile).setMetadata(metadata).build()imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this),object : ImageCapture.OnImageSavedCallback {override fun onError(exc: ImageCaptureException) {LogUtils.e(TAG, "Photo capture failed: ${exc.message}", exc)ToastUtils.shortToast(" 拍照失败 ${exc.message}")}override fun onImageSaved(output: ImageCapture.OutputFileResults) {val savedUri = output.savedUri ?: Uri.fromFile(photoFile)ToastUtils.shortToast(" 拍照成功 $savedUri")LogUtils.e(TAG, savedUri.path.toString())val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(savedUri.toFile().extension)MediaScannerConnection.scanFile(this@MainActivity,arrayOf(savedUri.toFile().absolutePath),arrayOf(mimeType)) { _, uri ->LogUtils.d(TAG,"Image capture scanned into media store: ${uri.path.toString()}")}}})}/*** 开始录像*/@SuppressLint("RestrictedApi", "ClickableViewAccessibility", "MissingPermission")private fun takeVideo() {//开始录像try {isRecordVideo = trueval mFileDateFormat = SimpleDateFormat(DATE_FORMAT, Locale.US)//视频保存路径val file =File(FileManager.getCameraVideoPath(), mFileDateFormat.format(Date()) + ".mp4")val outputOptions = OutputFileOptions.Builder(file)videoCapture?.startRecording(outputOptions.build(),Executors.newSingleThreadExecutor(),object : OnVideoSavedCallback {override fun onVideoSaved(outputFileResults: VideoCapture.OutputFileResults) {isRecordVideo = falseLogUtils.d(TAG, "===视频保存的地址为=== ${file.absolutePath}")//保存视频成功回调,会在停止录制时被调用ToastUtils.shortToast(" 录像成功 $file")}override fun onError(videoCaptureError: Int,message: String,cause: Throwable?) {//保存失败的回调,可能在开始或结束录制时被调用isRecordVideo = falseLogUtils.e(TAG, "onError: $message")ToastUtils.shortToast(" 录像失败 $message")}})} catch (e: Exception) {e.printStackTrace()LogUtils.e(TAG, "===录像出错===${e.message}")}}/*** 开始相机预览*/@SuppressLint("RestrictedApi")private fun startCamera() {cameraExecutor = Executors.newSingleThreadExecutor()val cameraProviderFuture = ProcessCameraProvider.getInstance(this)cameraProviderFuture.addListener(Runnable {cameraProvider = cameraProviderFuture.get()//获取相机信息//预览配置preview = Preview.Builder().build().also {it.setSurfaceProvider(viewFinder.surfaceProvider)}imageCamera = ImageCapture.Builder().setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY).build()videoCapture = VideoCapture.Builder()//录像用例配置.setTargetAspectRatio(AspectRatio.RATIO_16_9) //设置高宽比//.setTargetRotation(viewFinder.display!!.rotation)//设置旋转角度.build()try {cameraProvider?.unbindAll()//先解绑所有用例camera = cameraProvider?.bindToLifecycle(this,cameraSelector,preview,imageCamera,videoCapture)!!//绑定用例} catch (e: Exception) {LogUtils.e(TAG, "Use case binding failed", e.message)}}, ContextCompat.getMainExecutor(this))initListener()}override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults:IntArray) {super.onRequestPermissionsResult(requestCode, permissions, grantResults)when (requestCode) {Constants.REQUEST_CODE_PERMISSIONS -> {var allPermissionsGranted = truefor (result in grantResults) {if (result != PackageManager.PERMISSION_GRANTED) {allPermissionsGranted = falsebreak}}when {allPermissionsGranted -> {// 权限已授予,执行文件读写操作startCamera()}else -> {// 权限被拒绝,处理权限请求失败的情况ToastUtils.shortToast("请您打开必要权限")requestPermission()}}}}}private fun checkPermissions(): Boolean {when {Build.VERSION.SDK_INT >= 33 -> {val permissions = arrayOf(Manifest.permission.READ_MEDIA_IMAGES,Manifest.permission.READ_MEDIA_AUDIO,Manifest.permission.READ_MEDIA_VIDEO,Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO,)for (permission in permissions) {return Environment.isExternalStorageManager()}}else -> {for (permission in REQUIRED_PERMISSIONS) {if (ContextCompat.checkSelfPermission(this,permission) != PackageManager.PERMISSION_GRANTED) {return false}}}}return true}override fun onDestroy() {super.onDestroy()cameraExecutor.shutdown()}
}

15.实现的效果如下:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

16.日志打印:

模拟器日志如下:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
真机日志打印:
在这里插入图片描述
在这里插入图片描述

17.选择相册的代码如下:

package com.example.cameraxdemo.activityimport androidx.appcompat.app.AppCompatActivity
import android.content.ContentValues
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.widget.Button
import android.widget.ImageView
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.example.cameraxdemo.R
import com.example.cameraxdemo.utils.Constants.Companion.REQUEST_CODE_CAMERA
import com.example.cameraxdemo.utils.Constants.Companion.REQUEST_CODE_CROP
import com.example.cameraxdemo.utils.FileManager
import com.example.cameraxdemo.utils.FileUtil
import java.io.File/***@author: njb*@date:   2023/8/15 17:20*@desc:*/
class CameraActivity :AppCompatActivity(){private var mUploadImageUri: Uri? = nullprivate var mUploadImageFile: File? = nullprivate var photoUri: Uri? = nullprivate val btnCamera:Button by lazy { findViewById(R.id.btnCamera) }private val ivAvatar:ImageView by lazy { findViewById(R.id.iv_avatar) }private val TAG = CameraActivity::class.java.nameoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_camera)initView()}private fun initView() {btnCamera.setOnClickListener {startSystemCamera()}}/*** 调起系统相机拍照*/private fun startSystemCamera() {val takeIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)val values = ContentValues()//根据uri查询图片地址photoUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)LogUtils.d(TAG, "photoUri:" + photoUri?.authority + ",photoUri:" + photoUri?.path)//放入拍照后的地址takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)//调起拍照startActivityForResult(takeIntent,REQUEST_CODE_CAMERA)}/*** 设置用户头像*/private fun setAvatar() {val file: File? = if (mUploadImageUri != null) {FileManager.getMediaUri2File(mUploadImageUri)} else {mUploadImageFile}Glide.with(this).load(file).into(ivAvatar)LogUtils.d(TAG,"filepath"+ file!!.absolutePath)}/*** 系统裁剪方法*/private fun workCropFun(imgPathUri: Uri?) {mUploadImageUri = nullmUploadImageFile = nullif (imgPathUri != null) {val imageObject: Any = FileUtil.getHeadJpgFile()if (imageObject is Uri) {mUploadImageUri = imageObject}if (imageObject is File) {mUploadImageFile = imageObject}val intent = Intent("com.android.camera.action.CROP")if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)}intent.run {setDataAndType(imgPathUri, "image/*")// 图片资源putExtra("crop", "true") // 裁剪putExtra("aspectX", 1) // 宽度比putExtra("aspectY", 1) // 高度比putExtra("outputX", 150) // 裁剪框宽度putExtra("outputY", 150) // 裁剪框高度putExtra("scale", true) // 缩放putExtra("return-data", false) // true-返回缩略图-data,false-不返回-需要通过UriputExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()) // 保存的图片格式putExtra("noFaceDetection", true) // 取消人脸识别if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {putExtra(MediaStore.EXTRA_OUTPUT, mUploadImageUri)} else {val imgCropUri = Uri.fromFile(mUploadImageFile)putExtra(MediaStore.EXTRA_OUTPUT, imgCropUri)}}startActivityForResult(intent, REQUEST_CODE_CROP)}}override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {super.onActivityResult(requestCode, resultCode, data)if (resultCode == RESULT_OK) {if (requestCode == REQUEST_CODE_CAMERA) {//拍照回调workCropFun(photoUri)} else if (requestCode == REQUEST_CODE_CROP) {//裁剪回调setAvatar()}}}
}

18.选择相册后的效果如下:

在这里插入图片描述
在这里插入图片描述

19.总结:

今天虽然遇到了不少问题,但是出现问题后调试的过程很安逸,基本找出原因和解决花费了将近3个小时才把完整的demo整理出来,花费这么久的时间有三点原因:

1.Android13适配的规则没有搞清楚就直接升级更换了新版本,导致请求和拒绝后一直出错。

2.CameraX新的api和录像权限没看完整导致这里来回折腾了很久,最后找到了官网api才解决。

3.对于AGP8.1.0使用不熟悉,导致刚开始配置依赖也浪费了一点时间。

路漫漫而修远兮,吾将上下而求索,后面会把整理出来的完整适配Android13的例子也放出来,还有关于AGP8.1.0配置依赖方式变更的也会整理,遇到问题进行求索的过程比结果更重要,只要有一颗战胜困难的心,相信问题最终都会解决,如果感兴趣的可以尝试一下,若有其他问题可以提出来一起讨论解决,共同学习,一起成长.

20.demo源码地址如下:

https://gitee.com/jackning_admin/camera-xdemo

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/92678.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PHP实践:分布式场景下的Session共享解决方案实现

&#x1f3c6;作者简介&#xff0c;黑夜开发者&#xff0c;全栈领域新星创作者✌&#xff0c;CSDN博客专家&#xff0c;阿里云社区专家博主&#xff0c;2023年6月CSDN上海赛道top4。 &#x1f3c6;数年电商行业从业经验&#xff0c;历任核心研发工程师&#xff0c;项目技术负责…

LVS的负载均衡集群

基于四层协议进行 什么是集群 含义: 1、cluster 集群、群集 2、多台主机构成&#xff0c;但对外之表现为一个整体只提供一个访问入口&#xff08;域名和地址&#xff09;相当于一台大型计算机 目前互联网应用中&#xff0c;随着站点对硬件性能、响应速度、服务稳定性、数据…

使用 `tailwindcss-patch@2` 来提取你的类名吧

使用 tailwindcss-patch2 来提取你的类名吧 使用 tailwindcss-patch2 来提取你的类名吧 安装使用方式 命令行 Cli 开始提取吧 Nodejs API 的方式来使用 配置 初始化 What’s next? tailwindcss-patch 是一个 tailwindcss 生态的扩展项目。也是 tailwindcss-mangle 项目重要…

《起风了》C++源代码

使用方法 Visual Studio、Dev-C、Visual Studio Code等C/C创建一个 .cpp 文件&#xff0c;直接粘贴赋值即可。 #include <iostream> #include <Windows.h> #pragma comment(lib,"winmm.lib") using namespace std; enum Scale {Rest 0, C8 108, B7 …

【Linux】以太网协议——数据链路层

链路层解决的问题 IP拥有将数据跨网络从一台主机送到另一台主机的能力&#xff0c;但IP并不能保证每次都能够将数据可靠的送到对端主机&#xff0c;因此IP需要上层TCP为其提供可靠性保证&#xff0c;比如数据丢包后TCP可以让IP重新发送数据&#xff0c;最终在TCP提供的可靠性机…

SpringBoot后端服务开启Https协议提供访问(使用阿里云资源)

目录 概述 申请/下载证书 部署证书 本地测试访问 服务器部署访问 最后/扩展 总结 概述 本篇博客说明如何将SpringBoot项目开启Https协议提供访问。 博文以步骤【申请/下载证书】&#xff0c;【部署证书】&#xff0c;【本地测试访问】&#xff0c;【服务器部署访问】 &a…

【Java】BF算法(串模式匹配算法)

☀️ 什么是BF算法 BF算法&#xff0c;即暴力算法&#xff0c;是普通的模式匹配算法&#xff0c;BF算法的思想就是将目标串S的第一个与模式串T的第一个字符串进行匹配&#xff0c;若相等&#xff0c;则继续比较S的第二个字符和T的第二个字符&#xff1b;若不相等&#xff0c;则…

迭代器模式-遍历聚合对象中的元素

在开发中&#xff0c;我们经常使用到Iterator这个接口&#xff0c;我们很疑惑于这个接口的作用&#xff0c;认为集合已经实现了数据访问的方法&#xff0c;增加Iterator的意义在哪。本文我们将学习迭代器模式&#xff0c;用以探讨Iterator的作用。 1.1 迭代器模式概述 提供一…

LeetCode 160.相交链表

文章目录 &#x1f4a1;题目分析&#x1f4a1;解题思路&#x1f6a9;步骤一&#xff1a;找尾节点&#x1f6a9;步骤二&#xff1a;判断尾节点是否相等&#x1f6a9;步骤三&#xff1a;找交点&#x1f344;思路1&#x1f344;思路2 &#x1f514;接口源码 题目链接&#x1f449;…

LAXCUS分布式操作系统:技术创新引领高性能计算与人工智能新时代

随着科技的飞速发展&#xff0c;高性能计算、并行计算、分布式计算、大数据、人工智能等技术在各个领域得到了广泛应用。在这个过程中&#xff0c;LAXCUS分布式操作系统以其卓越的技术创新和强大的性能表现&#xff0c;成为了业界的佼佼者。本文将围绕LAXCUS分布式操作系统的技…

Jmeter 参数化的几种方法

目录 配置元件-用户自定义变量 前置处理器-用户参数 配置元件-CSV Data Set Config Tools-函数助手 配置元件-用户自定义变量 可在测试计划、线程组、HTTP请求下创建用户定义的变量 全局变量&#xff0c;可以跨线程组调用 jmeter执行的时候&#xff0c;只获取一次&#xff0…

LeetCode-101. 对称二叉树

Problem: 101. 对称二叉树 文章目录 思路解题方法Code结果 思路 看到这个题&#xff0c;想到的解题方法是使用递归实现。判断二叉树是否对称&#xff0c;需要判断根节点的左子树和右子树是否对称。所以从根节点开始&#xff0c;递归判断左子树的左节点是否和右子树的右节点是否…

【实战】十一、看板页面及任务组页面开发(一) —— React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目(二十三)

文章目录 一、项目起航&#xff1a;项目初始化与配置二、React 与 Hook 应用&#xff1a;实现项目列表三、TS 应用&#xff1a;JS神助攻 - 强类型四、JWT、用户认证与异步请求五、CSS 其实很简单 - 用 CSS-in-JS 添加样式六、用户体验优化 - 加载中和错误状态处理七、Hook&…

NPM与外部服务的集成(上)

目录 1、关于访问令牌 1.1 关于传统令牌 1.2 关于粒度访问令牌 2、创建和查看访问令牌 2.1 创建访问令牌 在网站上创建传统令牌 在网站上创建粒度访问令牌 使用CLI创建令牌 CIDR限制令牌错误 查看访问令牌 在网站上查看令牌 在CLI上查看令牌 令牌属性 1、关于访问令…

根据二叉树创建字符串

题目:给你二叉树的根节点 root &#xff0c;请你采用前序遍历的方式&#xff0c;将二叉树转化为一个由括号和整数组成的字符串&#xff0c;返回构造出的字符串。 空节点使用一对空括号对 "()" 表示&#xff0c;转化后需要省略所有不影响字符串与原始二叉树之间的一对…

centos7安装phpipam1.4

by:铁乐与猫 date&#xff1a;2021-5-11 安装依赖 sudo yum install epel-release sudo yum install php-mcrypt安装 Apache, MySQL, PHP (LAMP) stack packages sudo yum install httpd mariadb-server php php-cli php-gd php-common php-ldap php-pdo php-pear php-snmp …

Dubbo简介

1. Dubbo是什么&#xff1f; Dubbo是一个分布式服务框架&#xff0c;致力于提供高性能和透明化的RPC远程服务调用方案&#xff0c;以及SOA服务治理方案。简单的说&#xff0c;dubbo就是个服务框架&#xff0c;如果没有分布式的需求&#xff0c;其实是不需要用的&#xff0c;只…

【计算机网络】——数据链路层

二、组帧 1、字符计数法 帧头部使用一个字符来表示帧的大小(包括第一个计数字符) &#xff08;此处一字符一个字节&#xff09; 2、字符填充收尾定界法 特定字符来定界帧的首和尾。若帧中数据段出现等同于特定字符的字符内容&#xff0c;前置一个转义字符。(类似于正则表达…

微信小程序中键盘弹起输入框自动跳到键盘上方处理

效果展示 键盘未弹起时 键盘弹起后&#xff1a; 实现方式 话就不多说了 我直接贴代码了 原理就是用你点击的输入框的底部 距离顶部的位置 减去屏幕高度除以2&#xff0c;然后设成负值&#xff0c;再将这个值给到最外层相对定位的盒子的top属性&#xff0c;这样就不会出现顶…

jvm——垃圾回收机制(GC)详解

开始之前有几个GC的基本问题 什么是GC&#xff1f; GC 是 garbage collection 的缩写&#xff0c;意思是垃圾回收——把内存&#xff08;特别是堆内存&#xff09;中不再使用的空间释放掉&#xff1b;清理不再使用的对象。 为什么要GC&#xff1f; 堆内存是各个线程共享的空间…