从webview下载image / pdf

时间:2017-12-19 08:49:38

标签: android webview download

我想从WebView下载并分享图片或Pdf。对话框中有一个WebView,其中有两个Button用于下载,第二个用于共享。

 final Dialog dialog = new Dialog(v.getRootView().getContext());
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.activity_view_image_);
                dialog.setCancelable(true);
                dialog.show();

                wv = (WebView)dialog.findViewById(R.id.webView1);
                wv.getSettings().setLoadWithOverviewMode(true);
                wv.getSettings().setUseWideViewPort(true);
                wv.getSettings().setDomStorageEnabled(true);
                wv.getSettings().setJavaScriptEnabled(true);
                wv.getSettings().setAllowFileAccess(true);
                wv.loadUrl("http://example.com/documents/" + images[finalI] );              
 ImageButton download(ImageButton)dialog.findViewById(R.id.download);
                ImageButton share=(ImageButton)dialog.findViewById(R.id.share);

                download.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


              wv.setDownloadListener(new DownloadListener() {
 @Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
DownloadManager.Request request = newDownloadManager.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)a.getSystemService(DOWNLOAD_SERVICE);
                                        dm.enqueue(request);
                                        Toast.makeText(a.getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
                                    }
                                });
                        }
                    });

                share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });

            }

0 个答案:

没有答案