首页 > 解决方案 > 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

标签: androidmediastore

解决方案


试试这个代码:

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/


推荐阅读