java - 为什么 DownloadManager.Request "allowScanningByMediaScanner()"在 Android Q 上不起作用?

标签 java android android-10.0

下面的代码适用于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.

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

关于java - 为什么 DownloadManager.Request "allowScanningByMediaScanner()"在 Android Q 上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58385333/

相关文章:

android - 在键盘打开的情况下约束布局滚动到底部

java - E/Volley : [126] BasicNetwork. PerformRequest:意外响应代码 500

建立连接后,通过 WifiNetworkSpecifier 的 Android Q Wifi 连接立即失去连接

android - 如何在 Android 10 中创建公共(public)文件夹/文件

java - 错误 org.hibernate.util.JDBCExceptionReporter - 列 'type' 的数据被截断

java - 如果任务遇到异常,则会抑制其进一步执行。为什么?

java - 使用 Tycho 构建和部署仅更改的插件

java - 公共(public)存储库中的 Android In App Billing 公钥

android - android中的颜色混合

android-10.0 - Android Q Edge to Edge 应用程序内容未显示透明导航栏