android - Android Q保存媒体文件未显示在图库中

标签 android kotlin android-contentprovider android-10.0 android-mediascanner

我正在使用三星a51。
API等级10

我正在下载文件夹中保存其他类型的文件,例如-图像,视频,歌曲,zip


val filename = "exampleFileName.jpg"

            val now = System.currentTimeMillis() / 1000
            val values = ContentValues().apply {
                put(MediaStore.Downloads.DATE_ADDED, now)
                put(MediaStore.Downloads.DATE_MODIFIED, now)
                put(MediaStore.Downloads.DISPLAY_NAME, filename)
                put(MediaStore.Downloads.TITLE, filename.stripExtension())
                put(MediaStore.Downloads.SIZE, contentSize)
                put(MediaStore.Downloads.RELATIVE_PATH, "Download/my-appname")
                put(MediaStore.Downloads.IS_PENDING, 1)
            }

val uri: Uri? = null

waitForExecution(handler) {
   uri = resolver.insert(
    MediaStore.Downloads.getContentUri(volume),
    values
    )
}

val descriptor = resolver.openFileDescriptor(uri!!, "w")
if (descriptor != null) {
  val outputStream = FileOutputStream(descriptor.fileDescriptor)
  val written = writeFile(outputStream)
  if(written){
     values.clear()
     values.put(MediaStore.Downloads.IS_PENDING, 0)
     if(mimeType != null) values.put(MediaStore.Downloads.CONTENT_TYPE, mimeType)
     waitForExecution(handler) { // ui thread
      resolver.update(uri!!, values, null, null)
      // android says that content resolver automatically notify to system don't need to send brodcast.
      val intent = Intent(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE))
      context.sendBroadcast(intent)
      }
  }else{
     dontThrow { resolver.delete(uri!!, null, null) }
  }
} else {
   v("got null descriptor.")
}


一切都很好文件已成功写入。
文件显示在中文件管理器

但问题未在图库中显示。
在其他一些手机上也无法正常工作

它还显示在 Mx播放器

是否有任何通知该文件已添加

还是android 10 bug?

最佳答案

通过在插入时设置mimeType解决了问题

关于android - Android Q保存媒体文件未显示在图库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60811384/

相关文章:

android - Firebase - 未找到 GetTokenResult、getExpirationTimestamp()

android - 找不到 <自定义提供程序> 的提供程序信息

android - ios 10 的 Ionic Cordova 相机问题

android - firebase 等于多个值

java - 将 ListView 的选定项作为字符串或 TextView 获取时出现 ClassCastException

android - 像YouTube应用一样叠加显示

android - 找不到 org.jetbrains.kotlin :kotlin-stdlib-jdk8:1. 3.60-eap-25

android - kotlin变量函数和普通fun函数有什么区别?

android - CursorLoader 不显示数据库条目

android - 在多个 Android 应用程序之间共享和持久化数据