下面的代码
适用于Q以下的任何Android
版本。但是当我在Android
Q上测试它时,它不起作用。视频文件已下载并可播放。问题出在媒体扫描仪
上。
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();
}
}
最佳答案
这不起作用,因为此方法在 Android Q 中已弃用。
可以在官方文档中找到注释:
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.
关于java - 为什么 DownloadManager.Request "allowScanningByMediaScanner()"在 Android Q 上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58385333/