Search code examples
androidkotlinandroidxandroid-camerax

Having trouble saving images to a public directory [Kotlin]


So I hope the title makes sense but heres more context .

I want to make my own camera app and I am following this Google Practice for cameraX. I have got the camera working on my physical phone but the problem is I cant view my images that I have taken. Weird thing is the images appear in the gallery the next day and not right after taking a photo.

Now im not too sure why that is but after researching it seems that the file is not being saved in a public directory at least thats the conclusion I have succumbed to. This is the directory that the app saves to: /storage/emulated/0/Android/media/com.astro.openingcamera/openingCamera.

This is the block of code that produces the directory

    private fun getOutputDirectory(): File {
    val mediaDir = externalMediaDirs.firstOrNull()?.let {
        File(it, resources.getString(R.string.app_name)).apply { mkdirs() } }
    return if (mediaDir != null && mediaDir.exists())
        mediaDir else filesDir
}

So is there a away to modify this block so that it allows me to view the images right after taking the photo ? cause keep in mind the images dont appear in the gallery right after taking the photo.

Others have pointed to this documentation but I dont really understand it

Thanks in advance


Solution

  • I found the solution as suggested by blackapps I used a mediascanner to update the directory so heres my code

     MediaScannerConnection.scanFile(this ,
                arrayOf(outputDirectory.toString()),null,
        null)