在Android Kotlin / Java中使用自动刷新图库来在图库中保存图像和视频

时间:2019-06-19 11:22:01

标签: java android kotlin

我正在尝试将图像和视频保存在图库中。正在保存,但图库未自动刷新。如何使用自动刷新图库的方式保存保存在图库中的文件(图像/视频),就像whatsapp图像/视频保存一样。 这是我的保存功能: 当前文件可以是图像或视频

fun saveFile(currentFile:File,context:Context):Boolean {

val destPath = Environment.getExternalStorageDirectory()。absolutePath + DIRECTORY_TO_SAVE_MEDIA_NOW

     val destFile = File(destPath)
        checkFolder(destFile)

        try {
            FileUtils.copyFileToDirectory(currentFile, destFile)
            context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(destFile)))
        } catch (e: IOException) {
            return false
            e.printStackTrace()
        }
        return true
    }

    fun checkFolder(dir: File) {
        dir.mkdir()
        var isDirectoryCreated = dir.exists()
        if (!isDirectoryCreated) {
            isDirectoryCreated = dir.parentFile.mkdir()
        }
        if (isDirectoryCreated) {
            Log.d("Folder", "Already Created")
        }
    }

图片必须使用新的目录名保存在图库中。

0 个答案:

没有答案