【问题标题】:unable to open pdf in webview android app无法在 webview android 应用程序中打开 pdf
【发布时间】:2018-01-10 10:42:43
【问题描述】:

提前感谢您的帮助。我正在制作一个 android 应用程序,它基本上是一个 基于 webview 的应用程序。在这个应用程序中,一切正常,但有一点不正常,网站上的 PDF 文件无法打开。我不知道为什么?请帮我解决这个问题。

链接到我的源代码:- http://www.mediafire.com/file/zaab9i3pgx9cg3e/RefereshTest.zip

截图:

-Activity_Main XML

Android 清单 XML

Main_Activity Java

应用截图

提前致谢。请帮帮我。

视频链接:- http://www.mediafire.com/file/qnr3kxe0bbrhdk1/compressed.mp4

【问题讨论】:

  • 您好,请问您可以通过复制粘贴您的代码来替换前三个屏幕截图吗?将代码编辑为文本更容易。我也无法真正理解应用程序屏幕截图。我们应该在上面看到什么?谢谢你,欢迎使用 StackOverflow ;) !

标签: xml pdf webview android-studio-3.0


【解决方案1】:

我遇到了同样的问题并解决了:你必须在 web 视图中重写 onStartDownload 方法。

方法有:

强制下载并打开它:

 webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String aUrl, String userAgent, String contentDisposition, String mimetype, long contentLength) {

            fileName = URLUtil.guessFileName(aUrl, contentDisposition, mimetype); //returns a string of the name of the file THE IMPORTANT PART

                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(aUrl));
                    request.allowScanningByMediaScanner();
                    CookieManager cookieManager = CookieManager.getInstance();
                    String cookie = cookieManager.getCookie(aUrl);
                    request.addRequestHeader("Cookie", cookie);
                    request.setMimeType("application/pdf");
                    request.addRequestHeader("User-Agent", userAgent);
                    Environment.getExternalStorageDirectory();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    dm.enqueue(request);
                    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));





        }


    });

下载后不要忘记添加打开pdf的方法:

BroadcastReceiver onComplete=new BroadcastReceiver() {
        public void onReceive(Context ctxt, Intent intent) {
            openFile(fileName);        }
    };

    protected void openFile(String fileName) {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator +
                fileName);
        Uri path = Uri.fromFile(file);
        Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
        pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        pdfOpenintent.setDataAndType(path, "application/pdf");
        try {
            this.startActivity(pdfOpenintent);
        } catch (ActivityNotFoundException e) {
        }
    }

也不要忘记在 :

中声明权限
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

或者你只能

有意打开

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

两种解决方案都经过测试并且有效。

【讨论】:

  • 感谢您的帮助。这对我有用。你解决了我真正的大问题。再次,非常感谢.. :)
  • @Abhishek 那么你为什么不接受它作为答案并投赞成票!
  • 我这样做了,但是每当我点赞时就会出现此通知(“感谢您的反馈!声望低于 15 的人的投票会被记录下来,但不要更改公开显示的帖子得分。”)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多