通过文件 api 创建的文件在 mediastore api 中不可见Android API 29-30

问题描述

在我的应用程序中,创建了视频文件并将其保存到 Movies 目录中。为此,我使用了 File Api。

private val dir: File by lazy {
        File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),MOVIES_SUB_DIR_NAME
        ).apply { mkdirs() }
}

fun createFile(name: String): String = File(dir,name).absolutePath 

在 android api 29 中,我们切换到范围存储。现在他们开始使用 MediaStore Api。现在我正在通过 contentResolver.insert 创建文件。我也让他们通过他contentResolver.query

override fun getVideoMetaDataList(mimeType: String): List<MetaData> {
        val projection = arrayOf(
            MediaStore.Video.Media._ID,MediaStore.Video.Media.disPLAY_NAME,MediaStore.Video.Media.SIZE,MediaStore.Video.Media.DATE_MODIFIED,MediaStore.Video.Media.MIME_TYPE,MediaStore.Video.Media.DURATION
        )
        return context.contentResolver.query(
            MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY),projection,"${MediaStore.Video.Media.MIME_TYPE} LIKE ?",arrayOf(mimeType),null
        )?.use { cursor ->
            val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)
            val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.disPLAY_NAME)
            val sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)
            val dateModifiedColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_MODIFIED)
            val durationColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)

            mutablelistof<MetaData>().apply {
                while (cursor.movetoNext()) {
                    val id = cursor.getLong(idColumn)
                    add(
                        MetaData(
                            name = cursor.getString(nameColumn) ?: continue,uriString = ContentUris.withAppendedId(videoUri,id).toString(),size = cursor.getLong(sizeColumn),lastModification = cursor.getString(dateModifiedColumn) ?: continue,duration = cursor.getLong(durationColumn)
                        )
                    )
                }
            }
        } ?: emptyList()
}

但是通过 MediaStore Api 我看不到通过 FileApi 添加文件。这些是在我需要的应用程序的早期版本中创建的旧文件。他们只是不在 query 方法中返回

如果您在“文件”应用程序中更新文件(例如,更改名称),文件将变为可见

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)