Search code examples
androidmediastoreandroid-camerax

How to save to Media folder via Mediastore in CameraX?


I am still new to android programming. I am trying to take a photo using CameraX API. However, the sample code at Google Codelabs only saves the photo on the app's private directory in compliance with Android's Scoped Storage. I am not still knowledgeable with Mediastore so can you please lead me to a documentation on how to integrate Mediastore to CameraX?


Solution

  • CameraX provides API for saving to MediaStore. Please take a look at the ImageCapture#takePicture API. Code sample:

    val outputFileOptions = ImageCapture.OutputFileOptions.Builder(
        mContentResolver, 
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        contentValues).build();
    imageCapture.takePicture(outputFileOptions, ...