简体   繁体   English

在webview中下载文件?

[英]Download files in webview?

I have loaded a website in my WebView which allows downloading mp3. 我已经在WebView中加载了一个网站,该网站允许下载mp3。 However, you need to right click/long tap the download button and click " save link as " to download a mp3. 但是,您需要右键单击/长按下载按钮,然后单击“ 将链接另存为 ”以下载mp3。 If I just tap the download button, it starts streaming instead of downloading. 如果我只是点击下载按钮,它将开始流式传输而不是下载。

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;
    }

How do I force my code to download files instead of streaming? 如何强制我的代码下载文件而不是流式传输? :) :)

I dont know if it will help but,try adding these lines. 我不知道这是否有帮助,请尝试添加这些行。

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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM