【问题标题】:How to download web view contents like PDF or images?如何下载 PDF 或图像等 Web 视图内容?
【发布时间】:2016-08-12 21:50:07
【问题描述】:
     Button btn = (Button) findViewById(R.id.Savenotes);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);

            DownloadManager manager = (DownloadManager) getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
            manager.enqueue(request);
        }

我正在尝试下载 webview 中存在的内容。我正在使用下载管理器,但没有任何反应。

【问题讨论】:

    标签: android webview android-download-manager


    【解决方案1】:
    webView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }});
    

    【讨论】:

      【解决方案2】:
      webView.setDownloadListener(new DownloadListener()
        {
         public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
         {
          //for downloading directly through download manager
          Request request = new Request(Uri.parse(url));
          request.allowScanningByMediaScanner();
          request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
          request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
          DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
          dm.enqueue(request);
         }
        });
      

      【讨论】:

      • 对于 PDF,我使用的是 final String url = "docs.google.com/gview?embedded=true&url=" + myPdfUrl;并按下按钮没有发生任何事情
      • 你想在 webview 中下载文件还是点击按钮?或问题说你想从 webview 下载
      • on btn click 我要下载内存中的文件
      • 在 webview 中,我正在显示一个 pdf,点击按钮我想保存该 pdf。
      猜你喜欢
      • 2012-07-10
      • 2017-04-18
      • 2018-12-30
      • 2015-10-27
      • 1970-01-01
      • 2019-04-12
      • 2018-03-01
      • 1970-01-01
      • 2017-12-13
      相关资源
      最近更新 更多