如何在DownloadManager中设置下载目录?

时间:2014-10-15 00:51:52

标签: java android android-download-manager

我正在使用DownloadManager,但它将下载放在我不知道的地方。我想在sdcard中的“mp3”等特定文件夹中下载该文件。

以下是我正在使用的代码:

private class HelloWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url)
    {
        if(url.endsWith(".mp3") || !url.startsWith("http://xnm")) {
            String servicestring = Context.DOWNLOAD_SERVICE;
            DownloadManager downloadmanager;
            downloadmanager = (DownloadManager) getSystemService(servicestring);
            Uri uri = Uri.parse(url);
            DownloadManager.Request request = new Request(uri);
            Long reference = downloadmanager.enqueue(request);

            setProgressBarIndeterminateVisibility(false);
         }
         else {
             mWebView.loadUrl(url);
             setProgressBarIndeterminateVisibility(true);
         }

        return true;
    }
    @Override
    public void onPageFinished(WebView webview, String url){
        super.onPageFinished(webview, url);
        setProgressBarIndeterminateVisibility(false);
     }

}

提前致谢!!

3 个答案:

答案 0 :(得分:4)

使用

File file = new File(Environment.getExternalStorageDirectory(), "mp3")
request.setDestinationUri(Uri.fromFile(file));

设置下载路径

答案 1 :(得分:2)

实际上,经过大量搜索后我才开始工作,这里是现有代码。

File folder = new File(Environment.getExternalStorageDirectory() + "/any");
            if (!folder.exists()) {
                folder.mkdir();
            }
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDestinationInExternalPublicDir("/Download/Global Mp3", nameOfFile);

我们在清单文件中需要此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

多数民众赞成!也许它会帮助别人

答案 2 :(得分:0)

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);