将照片导出到屏幕截图不会将照片添加到图库

Exporting photo to Screenshots doesn't add the photo to the Gallery

我的目的是生成一张图片,用户稍后可以在他们的手机图库中看到它 phone。

为此,我使用以下代码:

private fun saveToInternalStorage(bitmap: Bitmap) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            saveBitmapAndroidQ(bitmap)
        } else {
            val imagesDir =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + File.separator + "Screenshots")
                    .toString()
            val image = File(imagesDir, "AppName_${UUID.randomUUID()}.jpg")
            val fos = FileOutputStream(image)

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            Objects.requireNonNull(fos).close();
            Log.d("exportation", "imagesDir: " + image.toString())
        }
    }

图像存储在以下 URI 中(我手动检查过它,观察它在我手机的内部存储中 phone):

/storage/emulated/0/Pictures/Screenshots/AppName_113a5010-fbef-41da-b4cc-88f94e25740e.jpg

但是,当我打开我的图库应用程序时,它没有出现在任何地方,既没有按名称查找它,也没有出现在“屏幕截图”文件夹中等。除了我用应用程序生成的这张新图像外,所有屏幕截图都出现了.

如何让图库显示我的图片?

另一方面,我希望能够在具有我的应用程序名称的图片中创建一个目录,例如:

/storage/emulated/0/Pictures/MyAppFolder/

要存储图像(并让它们出现在图库中),我如何创建自己的目录?

非常感谢您!

感谢 blackapps 指导我使用什么,这是我错过的:

清单

<manifest [...]>

[...]

    <!-- Refresh Media -->
    <protected-broadcast android:name="android.intent.action.MEDIA_MOUNTED" />

    <application [...]

        <activity [...]

            <!-- Refresh Media  -->
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_MOUNTED" />
                <data android:scheme="file" />
            </intent-filter>

        </activity>
    </application>
</manifest>
</manifest>

片段

    private fun addGallery(file: File) {
        MediaScannerConnection.scanFile(requireContext(), 
            arrayOf<String>(file.getAbsolutePath()),
            arrayOf("image/jpeg"),
            object : MediaScannerConnection.OnScanCompletedListener {
                override fun onScanCompleted(path: String?, uri: Uri?) {
                    // Blank
                }
            })
    }