为什么Environment.getExternalStorageDirectory返回错误的路径?

时间:2017-11-07 15:49:24

标签: android

我正在尝试使用WebView和DoanloadManager下载字体。我使用Environment.getExternalStorageDirectory() + "/Fonts/"将下载文件夹目录设置为“Fonts”文件夹,但我创建的是另一个不需要的目录,sdcard/storage/emulated/0/Fonts/。我的代码出了什么问题?以下是完整的代码段:

 @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
            String filename = URLUtil.guessFileName(url, contentDisposition, mimeType);
            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(url));
            request.setMimeType(mimeType);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            request.setDescription("Downloading file...");
            request.setTitle(filename);
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory() + "/Fonts/", filename);
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);

1 个答案:

答案 0 :(得分:0)

sdcard/storage/emulated/0/是您在使用模拟器时通常由Environment.getExternalStorageDirectory()返回的路径。

Environment.getExternalStorageDirectory()可以返回任何文件夹,但您无法真正预料到该路径。今天我们在同一设备上有多个配置文件。此外,这可以根据Android API版本进行更改。