if (status == DownloadManager.STATUS_SUCCESSFUL) {
//本地路径
val local =cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI))
Log.i("qwe", "local : " + local)
val localFile = File(Uri.decode(local.substring("file://".length)))
Log.i("qwe", "localFile exists: " + localFile.exists())
MediaScannerConnection.scanFile(
requireContext().applicationContext
, arrayOf(localFile.absolutePath)
, arrayOf("video/*")
){ path, uri->
Log.i("qwe", "onScanCompleted path: " + path)
Log.i("qwe", "onScanCompleted uri: " + uri)
viewModel.downloadSuccessLiveData.postValue(uri)
val contentValues = ContentValues()
contentValues.put(
MediaStore.Video.VideoColumns.BOOKMARK,
videoId
)
val ret = requireContext().contentResolver.update(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
contentValues,
MediaStore.Video.VideoColumns._ID +"=?",
arrayOf(uri.lastPathSegment)
)
Log.i("qwe", "update: " + ret)
}
}
上面是下载成功后的代码,视频文件确实是存在的,使用MediaScannerConnection.scanFile也可以获取到类似content://media/external/video/media/22293这样的uri,但就是在contentResolver.update或是query时出现了问题!
update返回的ret是0,query的cursor.count是0!感觉就像是没有同步到系统数据库!
然后奇怪的是去通知栏点击一下这条下载记录进行播放,就可以query到这个视频的信息了,这是什么原因?
非必现,但出现概率不低!
求助啊,有人知道这是啥情况么[捂脸]