xml - 无法在 webview Android 应用程序中打开 pdf

标签 xml pdf webview android-studio-3.0

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

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

屏幕截图:

-Activity_Main XML

Activity_Main XML

Android list XML

Android Manifest XML

Main_Activity Java

Main_Activity Java

应用程序屏幕截图

App ScreenShot

提前致谢。请帮助我。

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

最佳答案

我遇到了同样的问题并解决了它:你必须在 Web View 中重写 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);
    }
});

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

关于xml - 无法在 webview Android 应用程序中打开 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48185560/

相关文章:

android - Lollipop 应用程序的透明导航栏?

java - 在java servlet中使用itextpdf生成pdf

swift - 查看pdf文档

objective-c - 如何设置WebView的字体样式?

java - 在 Java 中将对象序列化为 XML 会忽略我的对象的一些变量字段

xml - 将 Liquibase XML 转换为 YAML?

python - 使用元素树 findall 解析 XML 命名空间

linux - 在 Linux 上将格式错误的 PDF 批量转换为 TIF

android - 在 webview 中阻止站点

android - Webview 应用程序出错