SaDeGH_F
SaDeGH_F

Reputation: 481

how to display downloaded video in Gallery?

I'm using this code for downloading a video from my server. but when download is done, video doesn't show up in device gallery.

String dst = "file:///mnt/sdcard/Bazu2.mp4";
Uri src_uri = Uri.parse("http://tiktakdl.tk/fvideo/bazu/Bazu2.mp4");
Uri dst_uri = Uri.parse(dst);

DownloadManager.Request req = new DownloadManager.Request(src_uri);
req.setDestinationUri(dst_uri);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(req);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(dst))));

I used ACTION_MEDIA_SCANNER_SCAN_FILE intent, but nothing happend. I think first I should check if file is downloaded completely and then use that intent but I don't know how.

Upvotes: 0

Views: 353

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

Enqueing download means it just starts so in 99,99% cases your broardcast is being sent far too early so basically you should not rescan unless download is complete as there's no point of doing so. To be notified of when your download complete listen to broadcast ACTION_DOWNLOAD_COMPLETE (docs) sent by Download Manager and then you should broadcast ACTION_MEDIA_SCANNER_SCAN_FILE.

Alternatively, which is cleaner solution (but it's API11+), you should rather use allowScanningByMediaScanner() (docs) prior enqueing your request:

request.allowScanningByMediaScanner()

to tell system to do that for you when download is completed.

Upvotes: 2

Related Questions