-
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public enumCaptureMediaContract.Mode
-
Constructor Summary
Constructors Constructor Description CaptureMediaContract(CaptureMediaContract.Mode mode, StreamFileManager fileManager, Function2<File, File, Unit> onFilesCreated)
-
Method Summary
Modifier and Type Method Description IntentcreateIntent(Context context, Unit input)final UnitrestoreFilePaths(String picturePath, String videoPath)Restores pictureFile and videoFile from previously persisted absolute paths. FileparseResult(Integer resultCode, Intent intent)-
-
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
-
createIntent
Intent createIntent(Context context, Unit input)
-
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
nullfor either parameter leaves the corresponding file reference unchanged.- Parameters:
picturePath- Absolute path of the photo destination file, ornull.videoPath- Absolute path of the video destination file, ornull.
-
parseResult
File parseResult(Integer resultCode, Intent intent)
-
-
-
-