Search code examples
androidkotlinandroid-external-storageandroid-internal-storage

Android - Saved image in external storage doesn't appear in gallery


I'm trying to copy images from internal to external storage by this method:

private val externalStorage = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)

override fun savePhotoToExternalStorage(file: File) {
    val toFile = File(externalStorage, file.name)
    file.copyTo(toFile, true)
    MediaScannerConnection.scanFile(context, arrayOf(toFile.path), null, null)
}

I see that image is saving in path /storage/emulated/0/Android/data/pl.renesans.renesans/files/Pictures/image.jpg and I can find it in my phone's files, but it doesn't appear in gallery. What am I doing wrong? I'm using wrong path to save this image or I'm using MediaScannerConnection.scanFile() in wrong way?


Solution

  • The MediaStore does not collect info about files in app specific folders.

    Hence no info from getExternalFilesdir().

    Gallery apps rely on the MediaStore.

    On Android below Q you can use getExternalStorageDirectory() and such.

    Or Environment.getExternalPublicDirectory(Environment.DIRECTORY_PICTURES).