Dani
Dani

Reputation: 85

ACTION_MEDIA_SCANNER_SCAN_FILE doesn't add file to MediaStorage always

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 :)

Upvotes: 2

Views: 3538

Answers (1)

Amir Hossein Mirzaei
Amir Hossein Mirzaei

Reputation: 2375

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/

Upvotes: 2

Related Questions