Android从SDcard本地在Webview中加载pdf

时间:2016-04-08 08:17:34

标签: javascript java android html webview

我正在尝试从我的sd加载pdf到webview。 我知道在webview中直接加载pdf是不可能的。所以我有一个html文件,它呈现文件并加载pdf。

来自 http://www.worldwidewhat.net/2011/08/render-pdf-files-with-html5/

样本

Android代码

    final WebView webView = (WebView) findViewById(R.id.magazineWebView);
    final ProgressBar spinner = (ProgressBar) findViewById(R.id.magazineSpinner);

    webView.setBackgroundColor(0xFFFFFFFF);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setSaveFormData(true);
    webView.getSettings().setBuiltInZoomControls(mgznCanZoom);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            spinner.setVisibility(View.GONE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });


String webPath   = "http://mysite/demo/pdf-js/index.htm"; //WORKS       
String file_name2   = "pdf-js/index.htm"; //LOAD HTML BUT NOT PDF

    String file_uri = MAGAZINE_FOLDER + file_name2;
    String sdPath = Uri.parse("file://"
                    + Environment.getExternalStorageDirectory()
                    + file_uri).toString();



    webView.loadUrl(sdPath); //webPath   
  
    

来自logcat的Erros:

  
I/chromium: [INFO:CONSOLE(0)] "XMLHttpRequest cannot load file:///storage/emulated/0/MyApp/Magazine/pdf-js/compressed.tracemonkey-pldi-09.pdf. Cross origin requests are only supported for protocol schemes: http, data, chrome, https.", source: file:///storage/emulated/0/MyApp/Magazine/pdf-js/index.htm (0)
I/chromium: [INFO:CONSOLE(52)] "Uncaught TypeError: Cannot read property 'Symbol(Symbol.iterator)' of null", source: file:///storage/emulated/0/MyApp/Magazine/pdf-js/lib/pdf.js (52)
I/AppLifecycle: onActivitySaveInstanceState

但是 - >  如果我将此文件存储在云中并按上面的方式加载它,它将正常工作。 任何人都知道为什么这不会在本地加载? 谢谢你们。

P.S我知道网上有很多这样的问题。我环顾四周,找不到解决办法。

我无法使用像https://github.com/JoanZapata/android-pdfview

这样的图书馆

0 个答案:

没有答案