2

I have the following line

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileOnDownload)));

Where fileOnDownload is the path of the file like this one:

/storage/emulated/0/Downloads/song.mp3

But sometimes when the broadcast is sent, it doesn't reload the MediaStore (on Android O at least)
You can see the full code here
I'm pretty new to Android MediaStore so don't be too rude, please :)

Jantzilla
  • 638
  • 1
  • 7
  • 19
Dani
  • 85
  • 1
  • 6
  • Possible duplicate of [Android: Refreshing the Gallery after saving new images](https://stackoverflow.com/questions/18624235/android-refreshing-the-gallery-after-saving-new-images) – Yoav Feuerstein Jan 29 '19 at 13:45

1 Answers1

2

try this code :

Intent intent = 
      new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);

and if above code dose not work you can try using MediaScannerConnection !

here is a good tutorial :

https://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/

Amir Hossein Mirzaei
  • 2,325
  • 1
  • 9
  • 17
  • I executed the code before making sure the file existed, that's why it worked sometimes – Dani Jun 11 '18 at 17:43
  • MediaScannerConnection should be preferred, according to other answers of similar questions - see here: https://stackoverflow.com/a/5814533/997940, https://stackoverflow.com/a/19867466/997940 – Yoav Feuerstein Jan 29 '19 at 13:44