Android-在外部存储中保存的图像未出现在图库中

问题描述

我正在尝试通过这种方法将图像从内部存储复制到外部存储:

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)
}

我看到图像保存在路径 /storage/emulated/0/Android/data/pl.renesans.renesans/files/Pictures/image.jpg 中,并且可以在手机的文件,但它不会出现在图库中。我究竟做错了什么?我使用错误的路径保存该图像,或者使用错误的方式使用MediaScannerConnection.scanFile()?

解决方法

MediaStore不会收集有关应用程序特定文件夹中文件的信息。

因此getExternalFilesdir()中没有信息。

图库应用程序依赖MediaStore。

在低于Q的Android上,您可以使用getExternalStorageDirectory()等。

Environment.getExternalPublicDirectory(Environment.DIRECTORY_PICTURES)