Android 在webview中下载文件?

Android 在webview中下载文件?,android,webview,Android,Webview,我在WebView中加载了一个允许下载mp3的网站。但是,您需要右键单击/长按下载按钮,然后单击“另存链接为”下载mp3。如果我只是点击下载按钮,它就会开始流媒体而不是下载 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_download); webView = (WebVie

我在WebView中加载了一个允许下载mp3的网站。但是,您需要右键单击/长按下载按钮,然后单击“另存链接为”下载mp3。如果我只是点击下载按钮,它就会开始流媒体而不是下载

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_download);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new MyWebViewClient());

    webView.getSettings().setLoadsImagesAutomatically(false);
    // download manager
            webView.setDownloadListener(new DownloadListener() {
                @Override
                public void onDownloadStart(String url, String userAgent,
                        String contentDisposition, String mimeType,
                        long contentLength) {
                    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(URLUtil.guessFileName(url, contentDisposition,
                            mimeType));
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(
                            Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                                    url, contentDisposition, mimeType));
                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    dm.enqueue(request);
                    Toast.makeText(getApplicationContext(), "Downloading File",
                            Toast.LENGTH_LONG).show();
                }
            });
            //download manager
    webView.loadUrl("http://www.beemp3s.org/");
}

private class MyWebViewClient extends WebViewClient { // handle URLs in
                                                        // webview only
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

如何强制代码下载文件而不是流式传输?:)

我不知道这是否有帮助,但是,尝试添加这些行

private class myWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.contains("beemp3.org")) {
        view.loadUrl(url);
    } else {
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(i);

    }


    return true;

}

不要让webview处理所有URL。您已经在扩展
shouldOverrideUrlLoading(…)
我只想使用我自己的下载管理器在我的webview中下载所有内容