Package 

Class CaptureMediaContract


  • 
    public final class CaptureMediaContract
    extends ActivityResultContract<Unit, File>
                        

    Activity result contract for capturing media (photos and/or videos) using the device camera.

    Files are created in external storage directories:

    • Photos: {externalFilesDir}/Pictures/

    • Videos: {externalFilesDir}/Movies/ With fallback to cache directories if external storage is unavailable.

    pictureFile and videoFile are set during createIntent and used by parseResult to resolve the captured file. After process death these references are lost; callers can restore them via restoreFilePaths before the pending result is dispatched.

    • Constructor Detail

      • CaptureMediaContract

        CaptureMediaContract(CaptureMediaContract.Mode mode, StreamFileManager fileManager, Function2<File, File, Unit> onFilesCreated)
        Parameters:
        mode - The capture mode determining what media types can be captured.
        fileManager - Manager for creating temporary files in external storage.
        onFilesCreated - Optional callback invoked inside createIntent after destination files are created, giving the caller an opportunity to persist the file references before the external activity starts.
    • Method Detail

      • restoreFilePaths

         final Unit restoreFilePaths(String picturePath, String videoPath)

        Restores pictureFile and videoFile from previously persisted absolute paths.

        After process death the in-memory file references are lost, so the pending ActivityResultContract callback would be unable to locate the captured media. Call this method before the result is dispatched (e.g. in onCreate) to reconstruct the destination files so that parseResult can return the correct file.

        Passing null for either parameter leaves the corresponding file reference unchanged.

        Parameters:
        picturePath - Absolute path of the photo destination file, or null.
        videoPath - Absolute path of the video destination file, or null.