3

The code below works for any Android version below Q. But when I test it on Android Q, it doesn't work. Video file is downloaded and playable. The problem is with media scanner.

private void downloadVideo(String url, String videoPath) {
        try {
            File file = new File(videoPath);

            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))
                    .setTitle(videoType.name() + " Video")
                    .setDescription("")
                    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                    .setDestinationUri(Uri.fromFile(file))
                    .setAllowedOverMetered(true)
                    .setAllowedOverRoaming(true);

            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, Uri.fromFile(file).getLastPathSegment());
            request.allowScanningByMediaScanner();

            DownloadManager downloadManager = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
            downloadManager.enqueue(request);

            Toast.makeText(this, "Download started. Check notification bar for progress.", Toast.LENGTH_SHORT).show();
        } catch (Exception ex) {
            ex.printStackTrace();
            Toast.makeText(this, "Unable to download video", Toast.LENGTH_SHORT).show();
        }
    }
Xihuny
  • 1,410
  • 4
  • 19
  • 48

1 Answers1

3

It won't work, because this method is deprecated in Android Q.

You can find a note in the official documentation:

Starting in Q, this value is ignored. Files downloaded to directories owned by applications (e.g. Context#getExternalFilesDir(String)) will not be scanned by MediaScanner and the rest will be scanned.

Source: https://developer.android.com/reference/android/app/DownloadManager.Request#allowScanningByMediaScanner()

  • 5
    Do you have any solution what to use instead – MrinmoyMk Mar 30 '20 at 08:04
  • Yes, it would be nice to have a solution that accompanies this answer – E.T. Apr 14 '20 at 21:47
  • You don't need to use anything instead. Just make sure that you aren't downloading files into directories owned by your application. As it said in that extract I provided files outside application directories will be scanned by MediaScanner without any additional calls. – Volodymyr Buberenko Apr 15 '20 at 19:43
  • @VolodymyrBuberenko MediaScanner not working in API 29. perhaps scanned with restart phone. – Farzad Dec 05 '20 at 16:55