android - Android Q 保存的媒体文件未显示在图库中
问题描述
我用的是三星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错误?
解决方案
通过在插入时设置 mimeType 解决了问题
推荐阅读
- javascript - 如何在 JavaScript 中使用 NOR 门?
- vb.net - 将常规查询转换为参数化查询的问题
- c# - DataGridView 中的 CheckBox 值始终为 true
- java - Java:直接将特定行(带有流)编辑/删除到原始文件中
- python - Python 2.7,在文件路径中使用用户名
- python - Python tkinter:创建动态下拉菜单并在选择后调用不同的操作
- php - 用户关闭页面时如何销毁会话?
- elasticsearch - ModuleNotFoundError:没有名为“elasticsearch”的模块
- r - 闪亮降级fontawesome 5到4
- javascript - Node.js - 将文件流从一台服务器传送到另一台服务器