Jag*_*gar 5 android kotlin android-camerax
我正在按照官方文档使用 CameraX 捕获视频,并且我想避免将捕获的视频保存在图库中。
现在我正在处理文档代码的这一部分:
// Create MediaStoreOutputOptions for our recorder
val name = "CameraX-recording-" +
SimpleDateFormat(FILENAME_FORMAT, Locale.US)
.format(System.currentTimeMillis()) + ".mp4"
val contentValues = ContentValues().apply {
put(MediaStore.Video.Media.DISPLAY_NAME, name)
}
val mediaStoreOutput = MediaStoreOutputOptions.Builder(this.contentResolver,
MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
.setContentValues(contentValues)
.build()
// 2. Configure Recorder and Start recording to the mediaStoreOutput.
val recording = videoCapture.output
.prepareRecording(context, mediaStoreOutput)
.withAudioEnabled()
.start(ContextCompat.getMainExecutor(this), captureListener)
Run Code Online (Sandbox Code Playgroud)
我注意到prepaeRecording()可以采用FileOutputOptions而不是MediaStoreOutput,所以我认为它可能是我应该工作的地方,但我没有找到任何 FileOutputOptions 和 CameraX 的示例,而且我希望它能够使用范围权限。
那可能吗?您能举个例子来避免将视频保存到图库吗?
我能够像下面这样实现它(在 Android 11 SDK 30 上测试):
val folder = File(getExternalFilesDir(Environment.DIRECTORY_DCIM).toString() + File.separator + "MyApp")
if (!folder.exists()) {
folder.mkdir()
}
val nomediaFile = File(folder.absolutePath + File.separator + ".nomedia")
if (!nomediaFile.exists()) {
nomediaFile.createNewFile()
}
val outputFile = File.createTempFile("SOME_NAME", ".mp4", folder)
val fileOutputOptions = FileOutputOptions.Builder(outputFile).build()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2042 次 |
| 最近记录: |