android - ACTION_MEDIA_SCANNER_SCAN_FILE 并不总是将文件添加到 MediaStorage
问题描述
我有以下行
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileOnDownload)));
fileOnDownload
像这样的文件的路径在哪里:
/storage/emulated/0/Downloads/song.mp3
但有时发送广播时,它不会重新加载MediaStore
(至少在 Android O 上)你可以在这里
看到完整的代码
我对 Android很陌生,所以请不要太粗鲁,请:) MediaStore
解决方案
试试这个代码:
Intent intent =
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
如果上面的代码不起作用,您可以尝试使用 MediaScannerConnection !
这是一个很好的教程:
https://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/
推荐阅读
- pointers - 在 Go 中设置一个指向 int 值的 int 指针
- c# - 是否可以将 BoxCollider2D 转换为 PolygonCollider2D?
- python - 在 matplotlib 中手动创建图例
- perl - 写入时复制以供参考
- python - python pandas DataFrame.apply:返回一个系列而不是一个数据框
- python - 如何在 iMX6 上使用 numpy?
- php - 如何为创建的每个新订单、产品等订阅 DrupalCommerce 2X 事件
- kotlin - 反序列化时自定义类上的杰克逊读取值上的 InvalidDefinitionException
- mysql - 如何在给定的表结构中创建月份分区
- forms - 有没有办法在“提交成功”的 AMP 表单中添加 AMP 列表?