【问题标题】:Exporting photo to Screenshots doesn't add the photo to the Gallery将照片导出到屏幕截图不会将照片添加到图库
【发布时间】:2020-09-03 22:36:13
【问题描述】:

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

为此,我使用以下代码:

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 中(我已经手动检查它,发现它在我手机的内部存储中):

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

但是,当我打开我的 Gallery 应用程序时,它不会出现在任何地方,既不会按名称查找,也不会在“屏幕截图”文件夹等中显示。除了我使用该应用程序生成的新图像之外,所有屏幕截图都会出现。

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

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

/storage/emulated/0/Pictures/MyAppFolder/

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

非常感谢您!

【问题讨论】:

  • 如果您重新启动设备,它会在一段时间后出现在“图库”中。
  • 您可以使用 File.mkdirs() 在图片中创建一个子目录。
  • 如果您希望您的图片立即在“图库”中可见,您必须为您的文件路径设置媒体扫描仪。

标签: android kotlin android-image android-gallery android-camera-intent


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2020-03-19
    • 2016-03-13
    • 1970-01-01
    • 2011-07-07
    相关资源
    最近更新 更多