如何下载PDF或图像等Web视图内容?

时间:2016-04-20 09:51:34

标签: android webview android-download-manager

     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中的内容。我正在使用下载管理器,但没有发生任何事情。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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