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();
}
}
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 (eg Context#getExternalFilesDir(String)) will not be scanned by MediaScanner and the rest will be scanned.
It was deprecated in Q. I'm still currently looking for the workaround
according to document https://developer.android.com/reference/android/app/DownloadManager.Request.html#allowScanningByMediaScanner()
maybe you can try the solution below. DownloadManager.addCompletedDownload() deprecated on Android Q
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.