How to capture a video with CameraX without saving the output to the Gallery?
Asked Answered
B

1

6

I am following the official documentation to capture a video with CameraX, and I want to avoid saving the captured video in the gallery.
For now I am working on this part of the documentation code:

// 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)

I noticed that prepaeRecording() can take FileOutputOptions instead of MediaStoreOutput, so I thought it could be where should I work but I have not found any example with FileOutputOptions and CameraX, and also I want it to work with the scoped permissions.
Is that possible? and can you help with an example to avoid saving the video to the gallery?

Burschenschaft answered 12/7, 2022 at 11:59 Comment(4)
you may know this, but it's quite easy to make a video recording much larger than available memory. This is why it's streamed to the file system. The only folder you will have access to would be your app's private folder. Android has either already taken or will be taking access to the general file system as of android 12.Sphygmic
@JohnLord but which folder you mean, I know about the limitation of /data/data/package_name one but is there a limitation for /storage/emulated/0/Android/data/package_name too?Burschenschaft
I should have mentioned you would still have access to the gallerySphygmic
Did you find a way? I have same codes like you and want to don't save device memory. @BurschenschaftCausey
B
5

I was able to achieve it like below (tested on 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()
Burschenschaft answered 12/7, 2022 at 19:30 Comment(2)
this is saving video inside of the app storage location, how can we save it to galleryCysticercus
@Ammar the one in question should save to galleryBurschenschaft

© 2022 - 2024 — McMap. All rights reserved.