【问题标题】:how can i download pdf on a webview in android? [duplicate]我如何在android的webview上下载pdf? [复制]
【发布时间】:2018-03-01 09:40:19
【问题描述】:

此方法是从活动的 oncreate() 调用的。为了使下载发生,我应该编写什么代码?目前,从这段代码中,webview 是可见的,最后有两个按钮。点击他们后下载不会发生。请帮忙

private void openBrowser() 
{
    Uri uri = Uri.parse("googlechrome://navigate?url=" + urlString + mSelectedProductDetails.getTransIdValue());
    Intent i = new Intent(Intent.ACTION_VIEW, uri);
    // final String message;
    if (i.resolveActivity(getActivity().getPackageManager()) == null) {
        i.setData(Uri.parse(urlString + mSelectedProductDetails.getTransIdValue()));
        message = urlString + mSelectedProductDetails.getTransIdValue();
    }else 
    {
        message = urlString+mSelectedProductDetails.getTransIdValue();}
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().getJavaScriptEnabled();
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        progress.setVisibility(View.INVISIBLE);
        webView.loadUrl(message);
    }
}

【问题讨论】:

标签: android


【解决方案1】:

要下载文件,您需要在 WebView 中实现 DownloadListener。

webView.setDownloadListener(new DownloadListener() {

    @Override
    public void onDownloadStart(String url, String userAgent,
                                String contentDisposition, String mimetype,
                                long contentLength) {

        mUrl = url;
        mUserAgent = userAgent;
        mContentDisposition = contentDisposition;
        mMimetype = mimetype;
        mContentLength = contentLength;

        if (isStoragePermissionGranted()) {
            downloadFile(url, userAgent, contentDisposition, mimetype, contentLength);
        }
    }
});

下载文件

public void downloadFile(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
    DownloadManager.Request request = new DownloadManager.Request(
            Uri.parse(url));

    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is 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", //To notify the Client that the file is being downloaded
            Toast.LENGTH_LONG).show();
}

【讨论】:

  • 一切都做得很好但没有帮助?有没有可能??
猜你喜欢
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多